- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why the types of formal parameters of this anonymous function can be inferred ?
Thu, 2010-04-29, 14:26
Hello, everyone.
Because I encountered strange behavior of scala's type checker
(2.7.7.final), I would like to know
the reason of the behavior.
In the following anonymous function expression, in spite of omissions
of the formal parameter
types of x and y, the type checker doesn't report type error and this
code successfully compiles.
val f = ((x, y) => "".replaceAll(x, y)) // f is typed as (String,
String) => String by scalac
... use f ...
However, according to the Scala Language Specification (2.7), all
formal parameter types must be explicitly
given if the expected type of the anonymous function is not
scala.FunctionN[...]. Now, it seems that
the expected type of the above anonymous function doesn't exist
because the type of f is not specified.
Why does this code compile? Am I missing something?