Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

lambda - Java 8: Target typing

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...