Scala中的java传递函数
由于Java 8,我可以通过
xyz.foreach(e -> System.out.println(e));
我可以做到以下几点
xyz.foreach(System.out::println)
我已经了解了this thread方法引用的工作原理,但问题如下:
Error:(16, 63) ambiguous reference to overloaded definition,
both method toJson in object IanesJsonHelper of type (source: IanesServer)String
and method toJson in object IanesJsonHelper of type (success: Boolean)String
match expected type Function[?,String]
val json = IanesJsonHelper.toJson(array,IanesJsonHelper.toJson _) ^
我有3个名为“toJSON”的函数
def toJson(id: Int): String
及
def toJson(success: Boolean): String
及
def toJson(source: IanesServer): String
最后一个是正确的
我在上面的错误消息中调用的函数是:
def toJson[T](source: Array[T], toJson: Function[T, String]): String
以下是相关代码:
val array = new Array[IanesServer](1)
array(0) = new IanesServer(1, "http://localhost:4567/", "Test")
val json = IanesJsonHelper.toJson(array,IanesJsonHelper.toJson)
我不明白我的错误是什么:
- 该数组的类型为IanesServer
- 调用方法中的T应该是IanesServer(数组[IanesServer]->;数组[T]
- 因为2。函数中的T必须与数组中的T相同,因此必须是函数[IanesServer,String]->;函数[T,字符串]
有人能不能请麻生太郎指出错误?目前我强烈反对函数是[?,String]。有什么想法吗
答复: 感谢您的快速回答,以下是我的选择:
IanesJsonHelper.toJson[IanesServer](array,IanesJsonHelper.toJson)
# 1 楼答案
您希望编译器推断
toJson
为Function[IanesServer, String]
类型,因为source
为Array[IanerServer]
类型,因此T
等于IanesServer
不幸的是,scala编译器没有那么聪明。有两种方法可以帮助编译器:
明确说明类型
IanesJsonHelper.toJson[IanesServer](array, IanesJsonHelper.toJson _)
将参数拆分为两个参数列表:
当您有两个参数列表时,传递给第一个列表的参数将告诉编译器如何绑定
T
,并且编译器将对其余列表使用这些绑定下面是另一个较短的示例:
这种行为可能看起来很不幸,但这是有原因的
Scala不同于Java或C#,它使用函数参数列表中的所有参数来计算它们的LUB(最小上界),并使用它来推断函数的泛型类型参数
例如:
在这里,scala使用两个参数(类型
Some[Int]
和None
)来推断A
(Option[Int]
-LUB)的类型。这就是为什么scala需要您告诉它您所指的toJson
重载另一方面,C#
最后一个注意事项:润滑非常棒,但也有它的disadvantages