PROPOSAL: Enhanced while statement

Joseph D. Darcy Joe.Darcy at Sun.COM
Sun Mar 29 22:41:08 PDT 2009


Marek Kozieł wrote:
> AUTHOR: Lasu aka Marek Kozieł
>
> OVERVIEW
>
> FEATURE SUMMARY:
> Enhanced while statement allow to iterate through iterator.
>
>   
[snip]

> Using while loop.
>
>
> EXAMPLES
>
> SIMPLE EXAMPLE:
>   String toString(Iterator<String> list){
>     if (!list.hasNext()) return "[]";
>     StringBuilder ret = new StringBuilder();
>     ret.append('[').append(list.next());
>     while (String string : list) {
>       ret.append(',').append(list.next());
>     }
>     return ret.append(']').toString();
>   }
>
> ADVANCED EXAMPLE:
>   String toString(Iterator<Transfer> list){
>     while (Transfer t : list) {
>       ArrayList<Data> data = new ArrayList<Data>();
>       if ( t.isOpeningTag() )
>         while (Transfer t : list){
>           if (t.isClosingTag()) break;
>           data.add(t.getData());
>         }
>       else {
>         list.remove(); // OK: no interactions
>         throw new Exception("...");
>         }
>       list.remove(); // warning list here can refer to last element of
> inner loop
>       // Process chunk
>     }
>
>   }
> DETAILS
>
> SPECIFICATION:
> Same as JLS 14.14.2, except:
> The Expression must either have type Iterator, or a compile-time error occurs.
> Let I be the type of the expression Expression
>
>
> If Expression is directly field or variable or parameter
> (ExpressionVariable) then additional checking is performed:
> If Variable is used as Expression for while-each loop inside
> while-each loop over same Variable then Variable occurring after inner
> while-each loop (but in outer while-each loop) will compile with
> warning “Variable state refer to inner loop”.
>
> COMPILATION:
> Same as for-each (almost).
>   

This is another vague and incomplete proposal.

-Joe



More information about the coin-dev mailing list