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

SAM types don't support type bounds #2732

Closed
Arthurm1 opened this issue Jun 11, 2017 · 5 comments
Closed

SAM types don't support type bounds #2732

Arthurm1 opened this issue Jun 11, 2017 · 5 comments

Comments

@Arthurm1
Copy link

The following compiles in Scala but fails in Dotty (using Scastie)

object Foo {
  val map = new java.util.HashMap[String, String]
  map.computeIfAbsent("hello", foo => "world")
}
-- Error: /tmp/scastie5756006742801572629/src/main/scala/main.scala:5:31 -------
5 |  map.computeIfAbsent("hello", foo => "world")
  |                               ^^^
  |                    missing parameter type for parameter foo, expected = ?
-- [E007] Type Mismatch Error: /tmp/scastie5756006742801572629/src/main/scala/main.scala:5:38 
5 |  map.computeIfAbsent("hello", foo => "world")
  |                                      ^^^^^^^
  |         found:    String("world")
  |         required: java.util.function.Function[_ >: String, _ <: String]#R         

trying

object Foo {
  val map = new java.util.HashMap[String, String]
  map.computeIfAbsent("hello", (foo: String) => "world")
}

still results in the second error

-- [E007] Type Mismatch Error: /tmp/scastie5756006742801572629/src/main/scala/main.scala:5:48 
5 |  map.computeIfAbsent("hello", (foo: String) => "world")
  |                                                ^^^^^^^
  |         found:    String("world")
  |         required: java.util.function.Function[_ >: String, _ <: String]#R
  |         
@smarter
Copy link
Member

smarter commented Jun 11, 2017

@adriaanm Does scalac do something special in its handling of SAMs to deal with types like Function<? super K,? extends V> coming from Java?

@adriaanm
Copy link
Contributor

adriaanm commented Jun 12, 2017

@adriaanm
Copy link
Contributor

We may have accumulated some more special-casing for existentials and type inference over the years, but I can't think of us doing anything specific to SAMs. This type checks too: def foo: T forSome {type T <: String} = "a"

@smarter
Copy link
Member

smarter commented Jun 12, 2017

But interestingly, this doesn't typecheck in either scalac or dotty:

scala> def foo: ({type T <: String})#T = "a"
<console>:11: error: type mismatch;
 found   : String("a")
 required: AnyRef{type T <: String}#T
       def foo: ({type T <: String})#T = "a"
                                         ^

@adriaanm
Copy link
Contributor

adriaanm commented Jun 12, 2017

Yeah, I think it's because the existential is extrapolated to its upper bound because it appears in a covariant position.

smarter added a commit to dotty-staging/dotty that referenced this issue Oct 18, 2017
The `isInstantiable` test as written is incorrect:
Given tp = `java.util.function.Function[String, _ <: String]`, we will have:
    tref = skolem TermRef with underlying info `tp`
    selfType = java.util.function.Function[tref#T, tref#R]` (because of the `asSeenFrom`)
`tref <:< selfType` is false, because `tref.underlying` is
just `tp` and its parameters are bounded wildcards, not references with
`tref` as a prefix.
In any case, I cannot think of a case where this function should return
false, and no test failed when I removed it. So I'm removing it until
someone can come up with a counter-example.
smarter added a commit to dotty-staging/dotty that referenced this issue Oct 18, 2017
The `isInstantiable` test as written is incorrect:
Given tp = `java.util.function.Function[String, _ <: String]`, we will have:
    tref = skolem TermRef with underlying info `tp`
    selfType = `java.util.function.Function[tref#T, tref#R]` (because of the `asSeenFrom`)
`tref <:< selfType` is false, because `tref.underlying` is
just `tp` and its parameters are bounded wildcards, not references with
`tref` as a prefix.
In any case, I cannot think of a case where this function should return
false, and no test failed when I removed it. So I'm removing it until
someone can come up with a counter-example.
smarter added a commit to dotty-staging/dotty that referenced this issue Oct 18, 2017
The `isInstantiable` test as written is incorrect:
Given tp = `java.util.function.Function[String, _ <: String]`, we will have:
    tref = skolem TermRef with underlying info `tp`
    selfType = `java.util.function.Function[String, tref#R]` (because of the `asSeenFrom`)
`tref <:< selfType` is false, because `tref.underlying` is
just `tp` and its return type parameter is a bounded wildcard, not a reference with
`tref` as a prefix.
In any case, I cannot think of a case where this function should return
false, and no test failed when I removed it. So I'm removing it until
someone can come up with a counter-example.
@allanrenucci allanrenucci changed the title Dotty doesn't infer types for java Function SAM types don't support type bounds Jan 2, 2018
smarter added a commit that referenced this issue Mar 23, 2018
Fix #2732: Allow wildcards in SAM types (take 2)
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