Java 8: Target typing

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.


Context

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;
    

Target type

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 your example

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.

Tags:

Java

Lambda