Thursday, 13 May 2021

Method determination ignored variable arity and implicit auto boxing/unboxing

Recently encountered a tweet by Simon Ritter. With a response by Brian Goetz.

Simon ritter:

TIL that determining method applicability initially ignores variable arity and implicit auto boxing/unboxing.

This code prints "long, long", which surprised me.

See section 15.12.2. of the Java Language Spec.

Brian Goetz:

The three-phase overload selection algorithm was added at the time autoboxing and varargs was added; if we had not done so, adding autoboxing/varargs would have been source-incompatible as it would have changed existing overload selection decisions.

The unfortunate consequences of trying to be backwards compatible.

References

[1] Java Language Spec 11 - 15.12. Method Invocation Expressions
https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.12

No comments:

Post a Comment