Home > Blockchain >  What does "providing a delegate" mean in the context of a lambda expression?
What does "providing a delegate" mean in the context of a lambda expression?

Time:11-15

I have a definition of the :: operator in Java as:

The double colon (::) operator, i.e. method reference operator, is used to call a method by referring to it with the help of its class directly. They behave the same as lambda expressions. The only difference is this uses direct reference to the method by name instead of providing a delegate to the method.

Can someone please help to explain the last part - i.e. "instead of providing a delegate to the method". What does this part mean?

CodePudding user response:

Delegation is when one object has a method which simply calls an equivalent method on another object; so for example, in the class below, the method foo is delegated to the object obj:

class Example {
    private final Bar obj;
    public Example(Bar obj) {
        this.obj = obj;
    }
    public Baz foo(Qux x) {
        return obj.foo(x);
    }
}

A lambda expression like x -> obj.foo(x) is syntactically similar to return obj.foo(x);, and the evaluation of the lambda expression creates an object with a method which delegates the foo method to the object obj, which is semantically similar to new Example(obj). So this is an example of delegation.

Of course, obj::foo also (semantically) creates an object with a method which delegates the foo method to the object obj. So the text you quoted seems debatable; the quote does at least say the behaviour is the same, so perhaps what the author means is that the lambda syntactically resembles delegation whereas the method reference does not (because it's in point-free style). At least, this seems to be what the quote means.


To pre-empt the debate about whether lambdas and method references really create objects: if you use a debugger then you might discover some implementation details which suggest otherwise, but the Java Language Specification says, authoritatively, that (§15.27.4)

At run time, evaluation of a lambda expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. [...] Either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced.

and (§15.13.3)

At run time, evaluation of a method reference expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. [...] either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced.

CodePudding user response:

It means that you can reference a method, for example like this:

myList.forEach(System.out::println);

... instead of having the write a lambda and make a call to the method:

myList.forEach(i -> System.out.println(i));
  • Related