Skip to content

Java higher-order methods inconsistently require type annotations #11558

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
joshlemer opened this issue Jun 4, 2019 · 5 comments · Fixed by scala/scala#8129
Closed

Java higher-order methods inconsistently require type annotations #11558

joshlemer opened this issue Jun 4, 2019 · 5 comments · Fixed by scala/scala#8129
Assignees
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) has PR infer java interop should compile usability
Milestone

Comments

@joshlemer
Copy link

This works:

scala> import java.util.stream.Stream
import java.util.stream.Stream

scala> Stream.of(1,2,3).map(_ + 1)
res20: java.util.stream.Stream[?0] = java.util.stream.ReferencePipeline$3@27014faf

But then, you'd think that this should work:

scala> Stream.of(1,2,3).map(_.toString)
                              ^
       error: type mismatch;
        found   : Int => String
        required: java.util.function.Function[_ >: Int, _]

It can be made to work only by including the map type param:

scala> Stream.of(1,2,3).map[String](_.toString)
res22: java.util.stream.Stream[String] = java.util.stream.ReferencePipeline$3@7e7daeac

This would hopefully work because it does actually work in java:

Stream.of(1,2,3).map(i -> i.toString());
@smarter smarter added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Jun 4, 2019
@SethTisue
Copy link
Member

SethTisue commented Jun 4, 2019

(quick, off-the-cuff reaction: reminiscent of scala/scala-dev#184 , is it the same?)

@dwijnand
Copy link
Member

dwijnand commented Jun 4, 2019

I guess the inconsistency is that + exists on Int, but .toString is on Object and Function isn't contravariant.

@joshlemer
Copy link
Author

@dwijnand no that's not the issue, it happens whenever the return-type of the function differs from the input type.

@SethTisue this does seem to be related or the same issue, yes.

@adriaanm
Copy link
Contributor

adriaanm commented Jun 5, 2019

I've been looking at this. Fertile ground for bug fixin'! Already found an issue introduced by scala/scala#6492, but no full fix for this issue yet. (WIP: https://github.com/adriaanm/scala/tree/t11558)

@som-snytt
Copy link

Seen on SO by someone "stuck" on 2.12, and not in the Valentine's Day sense of "stuck on you."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) has PR infer java interop should compile usability
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants