Since Java 8 I can pass intead of
xyz.foreach(e -> System.out.println(e));
I can do the following
xyz.foreach(System.out::println)
I have seen this thread about how method references work, but the problem is the following:
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 _) ^
I do have 3 functions with the name "toJSON"
def toJson(id: Int): String
and
def toJson(success: Boolean): String
and
def toJson(source: IanesServer): String
The last one is the right one.
The function I was calling in the error message above is:
def toJson[T](source: Array[T], toJson: Function[T, String]): String
This is the relevant code:
val array = new Array[IanesServer](1)
array(0) = new IanesServer(1, "http://localhost:4567/", "Test")
val json = IanesJsonHelper.toJson(array,IanesJsonHelper.toJson)
I do not get what my mistake is:
- The array is of type IanesServer
- T in the calling method should be IanesServer (Array[IanesServer] -> Array[T]
- Because of 2. the T in the function must be the same as the T in the array, therefore has to be Function[IanesServer,String] -> Function[T,String]
Can someone please be aso kind as to point out the mistake? At the moment I strongly disagree, that the function is [?,String]. Any ideas?
Answer: Thanks for the quick answer, here is what I chose:
IanesJsonHelper.toJson[IanesServer](array,IanesJsonHelper.toJson)