I'm reading a chapter on lambdas in Herbert Schildt's "Java: The Complete Reference" and there are quite a few references to "lambda target type" and "target type context":
A functional interface defines the target type of a lambda expression. Here is a key point: a lambda expression can be used only in a context in which its target type is specified.
Or:
As mentioned earlier, a lambda expression is not executed on its own. Rather, it forms the implementation of the abstract method defined by the functional interface that specifies its target type. As a result, a lambda expression can be specified only in a context in which a target type is defined. One of these contexts is created when a lambda expression is assigned to a functional interface reference. Other target type contexts include variable initialization, return statements, and method arguments, to name a few.
Yet another:
The functional interface associated with a lambda expression can be generic. In this case, the target type of the lambda expression is determined, in part, by the type argument or arguments specified when a functional interface reference is declared.
Can someone please help me understand what's meant by lambda target type?
For instance, in (int n) -> n % 2 == 0
is int
the lambda's target type?
Or in:
interface MyInterface<T> {
T func();
}
MyInterface<String> myInt = () -> { return "123"; }
What is the lambda's target type? Is it String
or MyInterface<String>
? And what is the lambda's context here?
I read several posts on SO on the topic but still cannot fully understand the notions.
Thanks.
One of the definitions of "target" (taken from here) is:
You can say the result a lambda expression intends to achieve is to implement some functional interface. Hence that functional interface can be seen as the target of that lambda expression, and the type of the functional interface is the target type.
Hence the target type is the type of the functional interface implemented by the lambda expression.
The target type can be inferred based on the context in which the lambda expression is used:
In
the target type is unknown. If you assign this expression to some functional interface reference, that would be the target type.
In
the target type is
MyInterface<String>
.You should understand "target type" as the functional interface as which the function is (intended to be) used.
Think about this: what is this lambda expression expected to be and how can it be used?
As the book notes it, this expression can't be used on its own. It needs to be associated with a functional interface.
Now, what functional interface can be the type of a lambda expression is picked up from the context. This is where the "target type" of the lambda expression means something.
Consider these examples:
Example 1:
You can call that with
In this case, you mean the type of
() -> "123"
to beSupplier<String>
. That's the target type of() -> "123"
in this context.Example 2:
As you can see, an identical lambda expression was used, but its target type is now
MyInterface<String>
.Likewise, you can declare another functional interface that has the same signature as
MyInterface.func()
and assign the very same lambda expression to it. The target type changes in these varied contexts.I decided to read a bit more about lamdas and found an excellent book "Beginning Java 8 Language Features: Lambda Expressions, Inner Classes, Threads, I/O, Collections and Streams" by Kishori Shiran.
I will just cite a few paragraphs: