-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java additions can silently break overloading extension methods #16743
Comments
That's the way it is, I'm afraid. Backward source compatibility can never be promised in the presence of extension methods. There could potentially be a warning at the definition of |
I also think we should warn when compiling an extension method for a value that already has a member with the same name; that's the only thing that can reasonably be done. |
A case that needs to be taken into account is the following trait Foo:
type X
extension (text: X) def indent: String
trait Bar extends Foo:
type X = String
extension (text: X) def indent: String = text In this case, we should not warn. This kind of cases can be found in |
|
There was one case exposed by the PR where Probably this happens more often than one might assume. The converse use case is that the spurious extension is a backup for cross-compiling against old API that lacks the method. |
Best effort to warn if defining extension method for a type which has a non-private member of the same name and a subsuming signature. Excludes opaque types (for defining forwarders to a public API) and other alias types (where the type member may override an abstract type member). Fixes #16743
I used Java 11 for a long time and I had an extension method
indent
onString
that indents a string.Java 12 and above introduced its own
indent
method. As a result, my existing code broke, like demonstrated in the following code.Compiler version
v3.2.2-RC2
Minimized code
Output
Under Java 12 and above:
Expectation
Print
hello
or at least give a warning/error that overloading existing class methods via extension methods is not possible.The text was updated successfully, but these errors were encountered: