2020-03-04

Java - Lambda expressions Target typing

Target typing

Lambda expressions don’t have type information. The actual type of a lambda expression is deduced by compiler from its surrounding context at compile time.

For example, lambda expression () -> System.out.println("Hello World!") can appear in any context where requires an instance of functional interface with the only abstract method takes no arguments and returns void. This could be java.lang.Runnable interface or any other functional interfaces created by third-party libraries or application code. So the same lambda expression can have different types in different contexts. Expressions like lambda expressions which have deduced type influenced by target type are called poly expressions.


Standalone expressions and Poly expressions 
Standalone expressions are expressions which have type that can be determined from the contents of expressions themselves. For example, a + b and str.substring(1) are standalone expressions. Poly expressions are expressions which have type that can be influenced by target type. Lambda expressions and method references are always poly expressions. Some other expressions may be poly expressions in certain cases.

No comments:

Post a Comment