Skip to content
Tags

,

JSR335: Poly Expressions

August 15, 2013

Poly expressions are expressions that may have different types in different contexts. Poly expressions are in contrast to standalone expressions whose type is known at compile time and fixed.

Where can I find poly expressions

Poly expressions may be any of these:

  • Parenthesized expressions – expressions that wrap poly expressions are also poly expressions.
  • Class instance creation expressions – <> (type inference) – Java SE 7 kind had this in assignment contexts
  • Method invocation expressions – you can pass “new ArrayList<>” as a parameter to a method argument and method argument inference will take care of the typing.
  • Conditional operator expressions – (? : ) – target type information is “passed” to all operands.
  • Lambda expressions – always poly expressions
  • Method and constructor references – always poly expressions

Conditional operators now support poly expressions as long as it’s a reference conditional expression. Conditional operators can be 1 of 3 possible expressions: boolean (if both 2nd & 3rd arguments are boolean), numeric (if both 2nd and 3rd arguments are numeric), otherwise they’re reference.

List<String> l = condition ? Arrays.asList() : Arrays.asList("a","b");

In Java 7 this was considered not well typed, but in Java 8 it is.

Future Uses

Currently, array initializers are called expressions, but they are not. They are language hacks. A good use of poly expressions would be to incorporate them into a new Collections creation syntax so they exist under the same umbrella.

Advertisements

From → Java

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: