def first[A] :Tuple2[A,_] => A = ( pair :Tuple2[A,_] ) => pair._1
val name = first( ("Anna", 23) )
“If you take a closer look at line 2, what you see here is a method call which returns a newly created function of type Tuple2[String,Any] => String (since the compiler kicks in and infers the needed type for applying to person). Although the whole expression looks like an ordinary method call, it’s in fact a method call (to a factory method without any parameter) and a function call which follows afterwards. ” — this is the explanation of the above code.
I am not able to reason about the first step of the above process (the process creating a function object). Can someone write out a “human compiler” procedure explicitly?
EDIT: I think the fully expanded logic for line 2 should be the following two lines
val firstAsFunc= first[String];
val name = firstAsFunc(("Anna", 23))
I’m not sure to break it down further. Here’s what I can think of — I hope you get it, or that someone else is feeling more clever than I.
The problem with the above is that
funcis really a getter — a method call itself — so I’m hardly changing anything.EDIT
I’m not sure what you mean by formal parameter. The method
firstdoesn’t have value parameters, just type parameters. Trying to pass a value parameter to it would be a syntactical error.