Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: Target typing

Tags:

java

lambda

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 of Callable. 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:

  1. target type
  2. context

I will really appreciate it if you also provide code snippets.

like image 896
Roushan Avatar asked Oct 18 '15 09:10

Roushan


People also ask

What is a target type in Java?

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.

What is target type?

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.

Is Java 8 lambda use parameter type inference?

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.

What does -> mean in Java?

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.


2 Answers

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.

like image 198
RealSkeptic Avatar answered Sep 20 '22 12:09

RealSkeptic


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.

like image 24
a3.14_Infinity Avatar answered Sep 18 '22 12:09

a3.14_Infinity