I can understand this:
scala> def f(i: Int) = "dude: " + i
f: (i: Int)java.lang.String
scala> f(3)
res30: java.lang.String = dude: 3
It defines a function f that takes an int and returns a string that is of the form dude: + the int that is passed in.
Now the same function can be specified like this:
val f: Int => String = x => "dude: " + x
scala> f(3)
res31: String = dude: 3
- Why do we need two
=> - What does
String = xmean? I thought that when you want to define something in Scala you’d dox:String?
You should parse it as
So it specifies that f has type
(Int => String)and is defined as an anonymous function which takes anIntparameter(x)and returns aString.