A function in Scala is an object that implements one of the features of FunctionN . For instance:
scala> def f(x: Int) = x * x f: (x: Int)Int scala> val ff = f _ ff: Int => Int = <function1> scala> val fff: Function1[Int, Int] = f _ fff: Int => Int = <function1>
So far so good. But what if we have a function that takes a by-name parameter? It certainly still implements one of the features of FunctionN :
scala> def g(x: => Int) = x * x g: (x: => Int)Int scala> val gg = g _ gg: => Int => Int = <function1> scala> gg.isInstanceOf[Function1[_, _]] res0: Boolean = true
But which type, exactly? This is not Function1[Int, Int] :
scala> val ggg: Function1[Int, Int] = g _ <console>:8: error: type mismatch; found : => Int => Int required: Int => Int val ggg: Function1[Int, Int] = g _ ^
And this is not Function1[Function0[Int], Int] :
scala> val ggg: Function1[Function0[Int], Int] = g _ <console>:8: error: type mismatch; found : => Int => Int required: () => Int => Int val ggg: Function1[Function0[Int], Int] = g _ ^
And Function1[=> Int, Int] failed to compile:
scala> val ggg: Function1[=> Int, Int] = g _ <console>:1: error: identifier expected but '=>' found. val ggg: Function1[=> Int, Int] = g _ ^
So what is it?
source share