传递函数由于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在调用方法中, d是IanesServer(Array [IanesServer] - > Array [T]
- 由于2.函数中的T必须与数组中的T相同,因此必须是Function [IanesServer,String] - > Function [T,字符串]
有人能指出错误吗?目前我强烈反对,该函数是[?,String]。有任何想法吗?
答: 感谢您的快速答案,这里是我选择:
IanesJsonHelper.toJson[IanesServer](array,IanesJsonHelper.toJson)
这很糟糕,Scala编译器不是那么聪明。足够。事情是,那(2)不起作用(IntelliJ抱怨,没有任何方法具有这种Siigneture)。 现在我正在使用“IanesJsonHelper.toJson [IanesServer](array,IanesJsonHelper.toJson)”。非常感谢! PS:感谢您编辑你的文章很多次:)有助于使事情更清晰(坦率地说,没有讽刺) –
@ Reisi007我知道,对吧?这对我来说也是令人惊讶的,尤其是来自C#的背景,在这里可以编译。尽管这个小小的怪癖,我仍然会选择Scala对C#的任何一天的类型推断。 – dcastro
我来自Java,并没有任何问题..(我猜,没有测试)。我只是习惯了Scala。无论如何感谢您的快速回答。非常appriciated:D –