Skip to content
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 higher-order methods inconsistently require type annotations #11558

Open
joshlemer opened this issue Jun 4, 2019 · 4 comments

Comments

Projects
None yet
5 participants
@joshlemer
Copy link
Member

commented Jun 4, 2019

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());
@SethTisue

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

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

@dwijnand

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

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

@joshlemer

This comment has been minimized.

Copy link
Member Author

commented Jun 4, 2019

@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

This comment has been minimized.

Copy link
Member

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)

@adriaanm adriaanm added this to the 2.13.1 milestone Jun 7, 2019

@adriaanm adriaanm self-assigned this Jun 7, 2019

@adriaanm adriaanm added the has PR label Jun 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.