On 19.10.2016 12:23, daniel_sun wrote:
Hi Jochen,
I plan to map Java's *::* to Groovy's *.&*, the following code is ok
now. What do you think about it?
[1, 2, 3].stream().forEach(System.out.&println) // object method,
[1, 2, 3].stream().forEach(System.out::println)
[1, 2, 3].stream().forEach(Objects.&requireNonNull) // class method, [1,
2,3].stream().forEach(Objects::requireNonNull)
The problem is that a method reference call should be something like a
tuple of (class, methodname, instance, arguments). The reference itself
is then (class, methodname, instance), which makes the call (class,
methodname, instance)(arguments). But it could also be: (class,
methodname) (instance, arguments).
Examples:
(1) Object::toString -> (Object, "toString") called with (instance,
arguments)
(2) x::toString -> (Object, "toString", x) called with (arguments)
(3) Foo::staticMethod -> (Foo, "staticMethod", null) called with (arguments)
you could say that (Object, "toString", x) is the same as (Object,
"toString"), with a curried x.
MethodClosure now offers (owner, name) with a call (arguments). owner is
either the instance or the class. As you see from this already, there is
a difference. This means we can do x.&toString (case 2 above) and we can
do Foo.&staticMethod (case 3 above), but for case 1 we are lacking
something.
Quite a lot of discussion and analysis has been done in
https://issues.apache.org/jira/browse/GROOVY-7772
bye Jochen