What will be the results of the following program?
Package Pzsobject Pz018 extends APP {def invert (v3:int) (V2:int = 2, V1:int = 1) {println (v1 + "," + v2 + "," + V3); } def INVERT3 = Invert (3) _ Invert3 (v1 = 2) invert3 (V1 = 2, v2 = 1)}
Explain
ETA expanded, missing information for default parameters
Explanation
The type of invert3 after Eta-expansion (sls§6.26.5) was no longer a method but a function object, i.e. an instance of fun Ction2[int, Int, Unit]. This was also reported by the REPL:
scala> def INVERT3 = Invert (3) _
INVERT3: (int, int) = = Unit
The This function object has a apply method (inherited from Function2[t1, T2, R]) with the following signature:
def apply (V1:T1, v2:t2): R
In particular, this method defines the no default values for its arguments! As a consequence, invert3 (v1 = 2) leads to a compile time error (as there is not enough actual arguments for method apply ).
The argument names V1 and V2 are the names as they is defined in the Apply-method of Function2[t1, T2, R]. The argument names of Apply-method of every function with both arguments is named V1 and V2, in particular these names is By no. means related to the argument names of method Invert3. Method Invert3 have by chance arguments with the same names, but unfortunately in a different order. Invert3 (v1 = 2, v2 = 1) Prints 1, 2, 3 as the parameter V1 (which corresponds to the parameter V2 in method invert) is set to 2 and parameter V2 (which corresponds to the parameter V1 in method invert) are set to 1.
The effect of the 18 partial function on the default parameters of the Scala fun topic