Why can't I implicit convert Scala's Function1 to java.util.function.Function?

隐身守侯 提交于 2019-12-21 18:05:37

问题


I'm trying to create a implicit conversion of Scala's Function1 to java.util.function.Function.

Here's my code:

object Java8ToScala extends App {

  implicit def javaFuncToScalaFunc[T, R](func1: Function[T, R]): function.Function[T,R] = {
    new function.Function[T, R] {
      override def apply(t: T): R = func1.apply(t)
    }
  }

  val javaFunc:function.Function[String,Int] = (s:String) => s.length

  println(javaFunc.apply("foo")) // this works

  private val strings = new util.ArrayList[String]()
  println(strings.stream().map(javaFunc).collect(Collectors.toList())) // this doesn't work

}

The compiler message is hard to understand:

[error] /xxx/Java8ToScala.scala:74: no type parameters for method map: (x$1: java.util.function.Function[_ >: String, _ <: R])java.util.stream.Stream[R] exist so that it can be applied to arguments (java.util.function.Function[String,Int])
[error]  --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error]  found   : java.util.function.Function[String,Int]
[error]  required: java.util.function.Function[_ >: String, _ <: ?R]
[error] Note: String <: Any, but Java-defined trait Function is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[error]     .map(javaFunc).collect(Collectors.toList()))
[error]      ^
[error] /xxx/Java8ToScala.scala:74: type mismatch;
[error]  found   : java.util.function.Function[String,Int]
[error]  required: java.util.function.Function[_ >: String, _ <: R]
[error]     .map(javaFunc).collect(Collectors.toList()))
[error]          ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 7 s, completed Dec 18, 2015 10:51:15 AM

回答1:


It's just Scala type inference failing, though I don't see why: it seems to be looking for an R which extends AnyRef. It works if you use such a type, e.g. val javaFunc: function.Function[String,String] = (s:String) => s.

However, it isn't getting an upper bound anywhere: using map[Int] explicitly works as well.




回答2:


Use the following implicit conversion:

implicit def javaFuncToScalaFunc[T, R](func1: function.Function[T, R]): Function[T,R] = {
  new Function[T, R] {
    override def apply(t: T): R = func1.apply(t)
  }
}

In your code, you have an implicit converstion from Scala function to Java function, but instead you should have an implicit conversion from Java function to Scala function.



来源:https://stackoverflow.com/questions/34356258/why-cant-i-implicit-convert-scalas-function1-to-java-util-function-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!