Scala's type inference for Functions


#1

Hello,

Consider the following code snippet

def f1(i:Int) = {println(s"$i")}
def f2(i:Int, j:Int) = {println(s"$i $j")}

//Following compiles
val a = f1(_)
//Following does not compile
val b = f2(_, 2)
//Following compiles
val b: Int=>Unit = f2(_, 2)

The question is, why can Scala infer the type of f1(_) correctly, while for f2(_ , 2) , it cannot?


#2

I think it’s a relatively arbitrary restriction. It works in Dotty.