`def time[A](f: => A)` what does this syntax mean

I came across this timer definition

def time[A](f: => A) = {
  val s = System.nanoTime
  val ret = f
  println("time: "+(System.nanoTime-s)/1e6+"ms")
  ret
}

Unfortunately I cannot find this construction (def time[A](f: => A)) in the reference. Can someone briefly explain the meaning and point to the place in reference, where it is described.

This is a by-name parameter.

1 Like

@sangamon Sorry, stupid question because I am very new to scala, in your link I do not see square brackets.

That is just a generic / type parameter: https://docs.scala-lang.org/tour/polymorphic-methods.html

1 Like

Scala has great free online courses. You can learn these basics (like type parameters, or by-name parameters) in this course: https://www.coursera.org/learn/scala-functional-programming from the language creator Martin Odersky himself.

1 Like

@spamegg1 Thank you, I have seen it. I do not need a certificate. I prefer learning by doing. To this end some comprehensive reference could be useful to have.

Let me illustrate why I think current online reference is not sufficient. Assume we would like to learn about the method groupBy. As the first approximation we type groupBy in the search field of the online language reference, and what we get: Trait Traversable and Trait Iterable. Why? This is not the kind of information one expects to see. One does not want to read about all the methods in the traits, but a concrete description and an example of the give method would be nice to have.

Next one searches in the API reference, and what comes out? Millions of results, which is not surprising because the method can be applied to different objects. But here again the description of what method is doing is missing.

In other words, the elements of the language are not only objects, traits, but also the respective methods. If one searches the method, one should land on the method description. If one searches for an object, one should land on the object description. Currently one searches a method and gets traits.

Any decent IDE has a shortcut for showing the java/scala-doc using some shortcut. In IDEA pressing CTRL+qwhile the cursor is on myCollection.groupBy shows me a nice popup with the relevant documentation.
image

1 Like

@andreak That’s the point. The content is available, it needs not be additionally created. It’s just the priority in which search results are displayed in the online reference!

I don’t quite see the problem; I searched for List in the online reference:

Clicked on List in the search-results:
image

There you can see groupBy and lots of other methods.

Searching for groupBy also works:

What exactly doesn’t work for you?

1 Like

@andreak You are right, with your help I can localize the description better. Everything works. However, it is my subjective opinion that

is disadvantage rather than advantage. I would not want to see lots of other methods, I would prefer to see just one and in greater details, possibly with examples. I know, it is meaningless to compare with other languages, but see the content of the GroupBy at the wolfram Mathematica. I would like to add, this is not criticism of the scala or its numerous contributors, I really appreciate the work of people in creating such a great language as scala, just my 5 cent vision.

I don’t know Mathematica, but in Scala groupBy is not “one thing”.

2 Likes