Skip to content

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

Closed
jvican opened this issue Dec 1, 2017 · 5 comments
Closed

SAM type does not support type bounds #3619

jvican opened this issue Dec 1, 2017 · 5 comments

Comments

@jvican
Copy link
Member

jvican commented Dec 1, 2017

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.

  def addIfMissing(from: AbsolutePath, computeBuild: AbsolutePath => Build): Unit =
    cache.computeIfAbsent(from, path => computeBuild(path))

Error:

[error] -- [E007] Type Mismatch Error: /data/rw/code/scala/loop/rewo
rk/src/main/scala/bloop/State.scala:29:58
[error] 29 |    cache.computeIfAbsent(from, path => computeBuild(pat
h))[error]    |
  ^[error]    |found:    bloop.io.AbsolutePath => bloop.Build
[error]    |required: java.util.function.Function[_ >: bloop.io.Abso
lutePath, _ <: bloop.Build]
[error]    |

If you replace path => computeBuild(path) for the shorter computeBuild or even computeBuild(_) 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.

@jvican
Copy link
Member Author

jvican commented Dec 1, 2017

I didn't explicitly say it, but the data structure backing up cache is a ConcurrentHashMap.

@jvican
Copy link
Member Author

jvican commented Dec 1, 2017

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)
    }
  }
}

@allanrenucci
Copy link
Contributor

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

@allanrenucci
Copy link
Contributor

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]
  |

@allanrenucci allanrenucci changed the title A Scala function does not adapt to a Java function SAM type does not support type bounds Dec 20, 2017
@smarter
Copy link
Member

smarter commented Dec 20, 2017

Duplicate of #2732. I tried and failed to make a proper fix for it in #3347. I agree that this is urgent and worth fixing.

@smarter smarter closed this as completed Dec 20, 2017
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

3 participants