Scala Java互操作-通配符类型的推断失败

Scala Java互操作-通配符类型的推断失败,scala,lambda,type-inference,Scala,Lambda,Type Inference,有没有什么方法可以编译(不显式地编写类型) 结果: error: 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]) --- be

有没有什么方法可以编译(不显式地编写类型)

结果:

error: 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])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : java.util.function.Function[String,Int]
 required: java.util.function.Function[_ >: String, _ <: ?0R]
Note: String <: Any, but Java-defined trait Function is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
                  l.stream.map(jfun(_.toInt))
                           ^
<console>:18: error: type mismatch;
 found   : java.util.function.Function[String,Int]
 required: java.util.function.Function[_ >: String, _ <: R]
                  l.stream.map(jfun(_.toInt))
                                   ^

错误:没有方法映射的类型参数:(x$1:java.util.function.function[\u>:String,\我认为您不能。Scala只有非常有限的向后推理(如果有的话)。如果您决定不进行全局推理,您将面临一个权衡(Scala没有).Scala无法从存在参数推断
R
map
。您可以调用
stream.map[Int](…)
error: 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])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : java.util.function.Function[String,Int]
 required: java.util.function.Function[_ >: String, _ <: ?0R]
Note: String <: Any, but Java-defined trait Function is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
                  l.stream.map(jfun(_.toInt))
                           ^
<console>:18: error: type mismatch;
 found   : java.util.function.Function[String,Int]
 required: java.util.function.Function[_ >: String, _ <: R]
                  l.stream.map(jfun(_.toInt))
                                   ^