limuhob limuhob - 4 months ago 10
Scala Question

scala - type of tuple with partial applied function

Can somebody explain me why the r1 type is:

(String => String, String)
however r2 type is
String => (String, String)
? Thank you.

def f1(n: String, m: String): String = m + n
val f2: String => String = f1(_, "something")
val r1: (String => String, String) = f2 -> "foo"
val r2: String => (String, String) = f1(_, "something") -> "foo"


Lets see what happens in place holder syntax for anonymous functions:

val f2: String => String = f1(_, "something")

It is expanded as: (x$1: String) => f1(x$1, "something")" (Start your repl with scala -Xprint:typer)

With f2 -> "foo", it simply becomes (f2,"foo") and hence (String => String, String)

With f1(_, "something") -> "foo", it is evaluated as:

(x:String) => f1(x,"something") -> foo
(x:String) => (f1(x,"something") , foo)
(String => (String, String))

If the confusion on why placeholder is evaluated first?

Placeholder is evaluated and the tree is adjusted at compile time. Where as with ->, it gets evaluated at runtime as tuple a due to ArrayAssoc implicit.