[Java Spec Report] Re: JSR 201 enhanced-for comments
Eric Blake <[email protected]> Mon, 23 Feb 2004 07:19:02 -0700
| Newsgroups | gmane.comp.java.spec-report |
|---|---|
| Message-ID | <[email protected]> |
One more idea. Is it reasonable to add an additional syntax for enhanced for
loops that allows one to reuse an existing variable rather than always
declaring a new one? This would parallel traditional for loops that allow you
to reuse an existing index variable rather than declare a new one. Here's an
example of what I mean:
class C {
public static void main(String[] args) {
String s = null;
for (s : args) {
if (s.equals("test")) break;
s = null;
}
System.out.println(s); // Will print either "null" or "test"
}
}
Basically, if the variable (it could even be a field, rather than a local
variable) already exists, the compiler can still perform type safety checks to
make sure the expression components can be assigned to the variable.
If you do decide to support something like this, you will need to consider the
use of arbitrary expressions that denote a variable (as opposed to a value):
class C {
public static void main(String[] args) {
for (args[0] : args); // Is this short for...
/* String[] a = args;
for (int i = 0; i < a.length; i++) {
args[0] = a[i];
;
}
*/
}
}
Eric Blake wrote:
> Below are my comments on enhanced for loops.
>
>>------------------------------------------------------------------------
>>
>>
>> An enhanced for loop for the Javaâ„¢ Programming Language
>>
>> Public Review Draft
>>
>>------------------------------------------------------------------------
--
Someday, I might put a cute statement here.
Eric Blake [email protected]
To unsubscribe from this mailing list, send an email to:
[email protected]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/java-spec-report/
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/