Skip to content

Ambiguous overload using JDK 11 #10715

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

Closed
noti0na1 opened this issue Dec 8, 2020 · 3 comments · Fixed by #16315
Closed

Ambiguous overload using JDK 11 #10715

noti0na1 opened this issue Dec 8, 2020 · 3 comments · Fixed by #16315

Comments

@noti0na1
Copy link
Member

noti0na1 commented Dec 8, 2020

Minimized code

The following code can be compiled using JDK 8. However, it will output ambiguous overload error when using JDK 11.

Environment:
Ubuntu
openjdk 1.8.0_275
openjdk 11.0.9.1

val decoderOut = java.nio.CharBuffer.allocate(1000)
val p = decoderOut.position

Output

-- [E051] Reference Error: Stest.scala:7:19 ------------------------------------
7 |val p = decoderOut.position
  |        ^^^^^^^^^^^^^^^^^^^
  |Ambiguous overload. The overloaded alternatives of method position in class CharBuffer with types
  | (x$0: Int): java.nio.CharBuffer
  | (): Int
  |both match expected type <?>

Expectation

The compiler shoud find and use position(): Int here.

@som-snytt
Copy link
Contributor

Same in Scala 2, as the non-nullary method is overridden for more specific return type. Workaround:

Starting scala3 REPL...
scala> val decoderOut: java.nio.Buffer = java.nio.CharBuffer.allocate(5)
val decoderOut: java.nio.Buffer =

scala> val p = decoderOut.position
val p: Int = 0

or just use parens.

@liufengyun
Copy link
Contributor

It can also be done with an explicit ():

val p = decoderOut.position()

Thus, I don't think this needs a fix.

@smarter
Copy link
Member

smarter commented Dec 14, 2020

I'm not sure, we're supposed to support calling Java methods without (), and even if we can't for some reason, the error message here is not very clear.

@smarter smarter reopened this Dec 14, 2020
odersky added a commit that referenced this issue Nov 17, 2022
#16315)

Fixes #10715.
Pair-programmed with @smarter this morning 😃

Parameterless alternatives were already preferred when there was no
matching alternativ (#6955).
With this PR, they are also preferred when the alternatives are
"ambiguous".
@Kordyjan Kordyjan added this to the 3.3.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants