Skip to content

Extension method conflicts with existing method from multiple supertypes #710

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

Open
EotT123 opened this issue May 18, 2025 · 2 comments
Open

Comments

@EotT123
Copy link
Contributor

EotT123 commented May 18, 2025

I added an extension method for org.w3c.dom.NodeList to provide a stream() method which works as expected. However, this approach breaks when trying to call stream() on instances of org.htmlunit.html.DomNodeList.

The issue is that DomNodeList extends both NodeList (which I've extended with stream()) and List, which already defines its own stream() method. As a result, the compiler fails with the following message: reference to stream is ambiguous

@rsmckinney
Copy link
Member

rsmckinney commented May 26, 2025

Adding discord discussion.

@rsmckinney
Copy link
Member

I will at least update the documentation to cover this use case. Probably an example like:

public interface Named {
  String getName();
}
public interface NickNamed {
  String getNickName();
}
public class Bob implements Named, NickNamed {
  public String getName() { return "Robert"; }
  public String getNickName() { return "Bob"; }
}
@Extension
public class NamedExt {
  public static String getNickName(@This Named thiz) { return thiz.getNamed().substring(0, 3); }
}

Bob bob = new Bob();
bob.getNickName(); // error: ambiguous
((NickNamed)bob).getNickName(); // "Bob"
((Named)bob).getNickName(); // "Rob"

This behavior is similar to C# and other languages that support multiple explicit interface implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants