Errors or strange behaviors in Scala 2.9

Note the following weird behavior (Scala 2.9.1.RC2):

scala> val spam = x => log(x) spam: Double => Double = <function1> scala> val spam = x => log(x)*log(x) <console>:10: error: missing parameter type val spam = x => log(x)*log(x) ^ scala> log(2)*log(2) res30: Double = 0.4804530139182014 

Why can Scala infer the type of the first, but not the second?

Another oddity:

 scala> def eggs(foo:Int=-1) = foo <console>:1: error: identifier expected but integer literal found. def eggs(foo:Int=-1) = foo ^ scala> def eggs(foo:Int= -1) = foo eggs: (foo: Int)Int 

What's going on here? Why is he suffocating when between = and -?

no space
+4
source share
1 answer

Question 1 . The surprise to me is that type inference succeeds at all. Another case that fails to compile is

 val spam = x => log(log(x)) 

Typically, the rule is that parameter types (here, x ) must be explicitly specified. But, obviously, this rule does not hold for the special case x => f(x) , which is rewritten to f _ . In other contexts, this rewriting leads to unspecified behavior .

Note. If the expected type of function exists, then the type of the parameter should not be explicit,

 val spam: Double => Double = x => log(log(x)) // OK 

Question 2 . Without a space, you came across the "operator" syntax for types. Here is an example that compiles,

 trait =-[A, B] trait One def eggs(foo: Int=-One) = foo 

It is equivalent

 def eggs(foo: =-[Int, One]) = foo 

The error message you received (expected identifier, but ...) says that integer literal 1 not a valid type.

+11
source

All Articles