This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why the types of formal parameters of this anonymous function can be inferred ?

No replies
Kota Mizushima
Joined: 2009-09-07,
User offline. Last seen 42 years 45 weeks ago.

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?

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland