Skip to content

Type inference not working with java overloads #12139

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
eatkins opened this issue Sep 4, 2020 · 5 comments
Closed

Type inference not working with java overloads #12139

eatkins opened this issue Sep 4, 2020 · 5 comments

Comments

@eatkins
Copy link

eatkins commented Sep 4, 2020

Forgive me if this is a duplicate, but there are cases where the scala compiler will infer Nothing where it is unexpected. This came up in playframework/playframework#10442. I was able to reproduce with 2.10.7, 2.11.11, 2.12.12 and 2.13.3.

reproduction steps

create a java file Foo.java:

public class Foo {
  void foo(Foo foo) {}
  void foo(java.util.Optional<Foo> foo) {}
}

create a scala file Bar.scala:

object Bar {
  Foo.foo(java.util.Optional.empty())
}

compile the project

problem

sbt:overload> compile
[info] Compiling 1 Scala source and 1 Java source to /Users/ethanatkins/work/scratch/overload/target/scala-2.12/classes ...
[error] /Users/ethanatkins/work/scratch/overload/src/main/scala/Bar.scala:2:7: overloaded method value foo with alternatives:
[error]   (foo: java.util.Optional[Foo])Unit <and>
[error]   (foo: Foo)Unit
[error]  cannot be applied to (java.util.Optional[Nothing])
[error]   Foo.foo(java.util.Optional.empty());
[error]       ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 1 s, completed Sep 4, 2020 8:10:45 AM

If I remove the overloaded foo method that takes a Foo, it does compile.

@eatkins eatkins changed the title [2.12.x] Type inference not working with java overloads Type inference not working with java overloads Sep 4, 2020
@som-snytt
Copy link

This is a known limitation and there are similar tickets which ticket fatigue prevents me from discovering.

Worth noting that if the overloads share the parameter of interest, inference is improved:

        public void foo(java.util.Optional<Foo> foo, Foo actual) {}
        public void foo(java.util.Optional<Foo> foo) {}

then

Bar.this.x.foo(java.util.Optional.empty[t12139.Foo]())   // not Nothing

The spec stipulates that arguments are typechecked without an expected type, but does not preclude adaptations after an overload is selected.

@eatkins
Copy link
Author

eatkins commented Sep 5, 2020

I'm not surprised to hear this is a known limitation. Feel free to close this if it's covered elsewhere.

@SethTisue
Copy link
Member

SethTisue commented Oct 12, 2020

Duplicate of #10542 and/or #10046? (At least, they look similar, offhand.)

@SethTisue
Copy link
Member

We can reopen if someone can substantiate that this one is actually different.

@som-snytt
Copy link

Seth is immune to ticket fatigue. Scala Center ought to present the Seth Tissue Award in Issuology in recognition of exceptional cross-ticketing.

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

4 participants