I looked at type inference in Scala, and there are a few things that I would like to understand a little better, why expression types / return methods must be explicitly declared in several cases.
Explicit Declaration return
Example (works if the keyword is returnomitted):
def upCase(s: String) = {
if (s.length == 0)
return s // COMPILE ERROR - forces return type of upCase to be declared.
else
s.toUpperCase()
}
Why can't I use an explicitly typed parameter as a return value without declaring the return type? And this is not only for references to direct parameters, just for any expression such as "output".
Method overload
Example (cannot compile when the second method is added joiner):
def joiner(ss: List[String], sep: String) = ss.mkString(sep)
def joiner(ss: List[String]) = joiner(strings, " ")
source
share