Skip to content

JSR335: Typing and Evaluation

August 17, 2013

It is a compile-time error if lambdas, method or constructor references occur in someplace other than an assignment context, an invocation context, or a casting context. The type of these are determined by the target type which depends on a match of function descriptors to a functional interface.

NOTE: A lambda expression must exactly match the function descriptors parameters and return type and the body must be assignment compatible with the descriptor’s return type. Also, a lambda expression’s expression body may return another poly expression.

Method and constructor references don’t need to be an exact match, instead the most specific declaration is used and is found by searching by parameter types.

Evaluation of poly expressions does NOT execute the expression; it only returns an instance of a functional interface. There is much freedom given in the JVM specification on how to do this to give room for efficient implementations. “For example, a separate synthetic class need not be defined for each distinct expression, nor must a new object be allocated on every evaluation.” (JLS8)

Also, all poly expressions MUST not throw checked exceptions (or super type exceptions) that do not appear in the function descriptor’s throws clause.

Generally speaking, a lambda of the form () -> expr, where expr is a statement expression, is interpreted as either () -> { return expr; } result compatible or () -> { expr; }, void compatible depending on the target type.

The equality operator is unpredictable when applied to the result of evaluation of a lambda expression: the equality test may produce different results in different implementations, or even upon different lambda expression evaluations in the same implementation. This is because the JVM implementors are free to choose to create new synthetic classes for each expression, or reuse similar ones.


From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: