While reading the article State of the Lambda I came to the topic Target Typing and I'm getting a bit confused with the following paragraph:
ActionListener l = (ActionEvent e) -> ui.dazzle(e.getModifiers());
An implication of this approach is that the same lambda expression can have different types in different contexts:
Callable<String> c = () -> "done"; PrivilegedAction<String> a = () -> "done";
In the first case, the lambda expression
() -> "done"
represents an instance ofCallable
. In the second case, the same expression represents an instance of PrivilegedAction.The compiler is responsible for inferring the type of each lambda expression. It uses the type expected in the context in which the expression appears; this type is called the target type. A lambda expression can only appear in a context whose target type is a functional interface.
Can you explain me these points in relation with the quoted paragraph in a simple way:
I will really appreciate it if you also provide code snippets.
The Java compiler takes advantage of target typing to infer the type parameters of a generic method invocation. The target type of an expression is the data type that the Java compiler expects depending on where the expression appears.
A target type is a category of related targets that share the same schemas. It defines the schema attributes that are common across a set of similar managed resources. Target types are profiles, or templates, that are used to create targets for specific instances of managed resources.
Type Inference means that the data type of any expression (e.g. method return type or parameter type) can be deduced automatically by the compiler. Groovy language is a good example of programming languages supporting Type Inference. Similarly, Java 8 Lambda expressions also support Type inference.
To support lambdas, Java has introduced a new operator “->”, also known as lambda operator or arrow operator. This arrow operator is required because we need to syntactically separate the parameter from the body. LambdaBody can be an expression or a block.
Context is the way an expression is used within the code. It's not just lambda expressions - it's any expression, like a+b
, a++
or Math.random()
.
Examples of possible contexts:
Assignment: take the expression a+b
. If you assign it to a variable, it is used in an assignment context:
c = a+b;
Argument to a method or constructor:. This is when you pass it to some method call:
System.out.println(a+b);
Return value: When you are using the expression in a return
statement:
return a+b;
Index to an array: When your expression is the index of an array:
x[a+b] = 3;
The target type is the type expected in the given context. For example, if you have a method defined as:
public int myMethod() { ... }
then any expression in a return
statement in its body is expected to have the type int
. So if you have this:
return a+b;
inside myMethod
, it's expected that a+b
will resolve to an int
or something that's assignable to an int.
Now, suppose you have this method:
public void anotherMethod( double d );
Then when you call it, and pass an expression as an argument, that expression is expected to be of type double
. So a call like:
anotherMethod(a+b);
expects a+b
to resolve to a double
. That's its target type.
In the declaration:
Callable<String> c = () -> "done";
the expression is the lambda expression () -> "done"
. It is used in an assignment context (it is assigned to c
). And the target type is Callable<String>
because that's what is expected when you assign anything to c
.
For a more formal discussion, refer to the Java Language Specification, Chapter 5.
A lambda expression is an instance of a functional interface. But a lambda expression itself does not contain the information about which functional interface it is implementing; that information is deduced from the context in which it is used. For example, the expression
x -> 2 * x
can be an instance of the functional interface
interface IntOperation { int operate(int i); }
so it is legal to write
IntOperation iop = x -> x * 2;
The type expected for the expression on the right-hand side of the assignment here is IntOperation. This is called the target type for the lambda expression. Clearly a lambda expression can be type-compatible with different functional interfaces, so it follows that the same lambda expression can have different target types in different contexts. For example, given an interface
interface DoubleOperation { double operate(double i); }
it would also be legal to write
DoubleOperation dop = x -> x * 2;
The target type for a lambda expression must be a functional interface and, to be compatible with the target type, the lambda expression must have the same parameter types as the interface’s function type, its return type must be compatible with the function type, and it can throw only exceptions allowed by the function type.
Courtesy: What is the type of a lambda expression?
Let me add a resource regarding Lambdas. This seems to be very helpful in understanding clearly many concepts in context of Lambdas. The resource is LAMBDA FAQ. {Disclaimer: I am in no way related to the website. I just happened to visit the site, so I wanted to recommend it, so that people could benefit out of it.} Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With