Suma
Suma

Reputation: 34463

Why does a function literal used as a default argument require a parameter type?

This compiles fine:

def walk[X](a: X)(f: X => Boolean): Boolean =  f(a)

walk(1)(_ => true)

This compiles fine as well:

def walk(a: Int)(f: Int => Boolean = _ => true): Boolean =  f(a)

walk(1)()

This does not:

def walk[X](a: X)(f: X => Boolean = _ => true): Boolean = f(a)

walk(1)()

The error is:

Error:(1, 38) missing parameter type

The obvious workaround is to use (_: X) => true, but why is this an error? I though that when the second parameter list is being processed, type information obtained from the first one should already be available?

Tested with Scala 2.11.8 and 2.12.1

Upvotes: 3

Views: 117

Answers (1)

slouc
slouc

Reputation: 9698

Looks like this issue. Note that this is the case only for default parameters; elsewhere the inference works just fine.

For example,

// works
def walk1[X]: X => Boolean = _ => true

// fails
def walk2[X](f: X => Boolean = _ => true) = ???

Upvotes: 2

Related Questions