简体   繁体   English

为什么我不能将Scala的Function1隐式转换为java.util.function.Function?

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

I'm trying to create a implicit conversion of Scala's Function1 to java.util.function.Function. 我正在尝试创建Scala的Function1到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

It's just Scala type inference failing, though I don't see why: it seems to be looking for an R which extends AnyRef . 这只是Scala的类型推断失败了,虽然我不明白为什么它似乎是在寻找一个R延伸AnyRef It works if you use such a type, eg val javaFunc: function.Function[String,String] = (s:String) => s . 如果使用这样的类型,它可以工作 ,例如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. 但是,它没有在任何地方获得上限:使用map[Int]明确地起作用

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. 在您的代码中,您有一个从Scala函数到Java函数的隐式转换,但您应该从Java函数到Scala函数进行隐式转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有标准的方法来转换 java.util.function,Consumer<T> 进入一个 java.util.function.Function<T, Void> - Is there a standard way to convert a java.util.function,Consumer<T> into a java.util.function.Function<T, Void> Java.util.function.Function带参数 - Java.util.function.Function with parameter 将java.util.function.Function转换为Interface - Cast java.util.function.Function to Interface 实现java.util.function.Function时是否需要检查空输入<T,R> - Do I need to check for null input when implementing java.util.function.Function<T,R> 为什么guava版本21.0中的地图不将java.util.function.Function等作为参数? - Why doesn't Maps in guava version 21.0 take java.util.function.Function etc as parameters? 关于Java泛型和java.util.function.Function设计的问题 - Question about Java generics and design of java.util.function.Function Scala 功能1<t,u> 变成 Function1<object,object> 在 Java</object,object></t,u> - Scala Function1<T,U> becomes Function1<Object,Object> in Java 用于按输入分组的通用 java.util.function.Function - Generic java.util.function.Function for Grouping as per input 将 java.util.function.Function 定义为 ZA81259CEF8E5559C6297DF1D4 - Defining java.util.function.Function as static final 使用java.util.function.Function实现Factory Design Pattern - use java.util.function.Function to implement Factory Design Pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM