-
Notifications
You must be signed in to change notification settings - Fork 1.1k
SAM type does not support type bounds #3619
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
Labels
Comments
I didn't explicitly say it, but the data structure backing up |
This is a normal workaround people can use: object Compat {
implicit class JavaFunction[T, R](f: T => R) {
import java.util.function.Function
def toJava: Function[T, R] = new Function[T, R] {
def apply(t: T): R = f(t)
}
}
} |
We do support SAM types. This is a bug. Here is a minimised version class MyMap[K, V] {
def computeIfAbsent(key: K, mappingFunction: java.util.function.Function[_ <: K, _ <: V]) = ()
}
class Test {
val map = new MyMap[Int, Int]
map.computeIfAbsent(1, (x: Int) => x)
} -- [E007] Type Mismatch Error: tests/allan/Test.scala:7:38 ---------------------
7 | map.computeIfAbsent(1, (x: Int) => x)
| ^
| found: Int => Int
| required: java.util.function.Function[_ <: Int, _ <: Int]
|
one error found |
The issue arises when the SAM type has type bounds class Test {
def foo(x: java.util.concurrent.Callable[_ <: Int]) = ???
foo(() => 1)
} -- [E007] Type Mismatch Error: tests/allan/Test.scala:3:13 ---------------------
3 | foo(() => 1)
| ^
|found: scala.this.Function0[scala.this.Int]
|required: <root>.this.java.util.concurrent.Callable[_ <: <root>.this.scala.Int]
| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With SAMs in 2.12 the following works, but it fails in Dotty. I think this is Dotty's fault, correct me if I'm wrong.
Error:
If you replace
path => computeBuild(path)
for the shortercomputeBuild
or evencomputeBuild(_)
it also fails. I guess this is because Dotty does not have support for SAM types?Scala 2 mode is enabled, didn't try without it.
The text was updated successfully, but these errors were encountered: