SAM conversion in for expressions

Thomas Jung thomas.andreas.jung at googlemail.com
Sat Aug 7 12:16:04 PDT 2010


Hi,

according to the language specification only iterables and arrays are
allowed in a for expression:

>The enhanced for statement has the form:
>
>EnhancedForStatement:
>      for ( VariableModifiersopt Type Identifier: Expression) Statement
>The Expression must either have type Iterable or else it must be of an array type (§10.1), or a compile-time error occurs.

Iterator<String> iterator = Collections.singleton("a iter").iterator();
Iterable<String> i = #(){ iterator };
for(String s : i) System.out.println(s);
for(String s : #(){ iterator }) System.out.println(s);

So for the given example only the first expression compiles although
the information needed to infer the type is present. I think this is
more a matter of consistency than an useful construct.

Thomas


More information about the lambda-dev mailing list