Class C {def sum (x:int = 1, Y:int = 2): Int = x + y} class D extends C {override def sum (y:int = 3, X:int = 4): Int = Super.sum (x, y)} val d:d = new D val c:c = d c.sum (x = 0) d.sum (x = 0)
Explanation
Scala uses the static type of a variable to bind parameter names, but the defaults is determined by the runtime type:
The binding of parameter names is do by the compiler and the only information the compiler can use is the static type of The variable.
For parameters with a default value the compiler creates methods which compute the default argument expressions (sls§4.6) . In the above example, both classes C and D contain the methods sum$default$1 and sum$default$2 for the default Paramet ERs. When a parameter was missing, the compiler uses the result of these methods, and these methods are invoked at Run-time.
Scala puzzle 22 Function Rewrite, positional parameter, parameter name