In Scala, why can’t I partially apply a function without explicitly specifying its argument types?

References below are to the Scala Language Specification

Consider the following method:

def foo(a: Int, b: Int) = 0

Eta Expansion can convert this to a value of type (Int, Int) => Int. This expansion is invoked if:

a) _ is used in place of the argument list (Method Value (§6.7))

val f = foo _

b) the argument list is omitted, and expected type of expression is a function type (§6.25.2):

val f: (Int, Int) => Int = foo

c) each of the arguments is _ (a special case of the ‘Placeholder Syntax for Anonymous Functions’ (§6.23))

val f = foo(_, _)   

The expression, foo(_, 1) doesn’t qualify for Eta Expansion; it just expands to (a) => foo(a, 1) (§6.23). Regular type inference doesn’t attempt to figure out that a: Int.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)