X10

Multi-init / multi-update 'for' loops, a la C

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: Language Design
  • Labels:
    None
  • Number of attachments :
    0

Description

Two people have recently noted the lack of full C-style for-loops to me in disparaging tones. That is, we can't say:

for( var i : Int = 0, var j : Int = 100; i < j; i++, j--) { ... i ... j ... }

I don't know that it's a big deal – I personally use while loops for this sort of thing, even in C – but there is some value to not gratuitously differing from C standards in small ways. Completely disregarding C is fine, but if we've got something that's 95% C-compliant anyways and there is no reason not to do the remaining 5% save our own good taste, we might as well do the last 5% and good taste be damned.

Activity

Hide
Igor Peshansky added a comment -

As noted in the comment to XTENLANG-1173, the correct syntax for this is:

for (var i:Int = 0, j:Int = 100; i < j; i++, j--) { ... i ... j ... }

It could be documented more prominently, of course.

Show
Igor Peshansky added a comment - As noted in the comment to XTENLANG-1173, the correct syntax for this is:
for (var i:Int = 0, j:Int = 100; i < j; i++, j--) { ... i ... j ... }
It could be documented more prominently, of course.
Hide
Bard Bloom added a comment -

No, that's not quite right. Maybe it's at 99%, but not 100%.
In C-style, you can do the following:

for(shared var i:Int = 1, val x = new Fue(), x.willUse(()=>i), ; i < 100; i++) ...

and in X10 you can't: you can't have multiple statements for the init clause.

Show
Bard Bloom added a comment - No, that's not quite right. Maybe it's at 99%, but not 100%. In C-style, you can do the following:
for(shared var i:Int = 1, val x = new Fue(), x.willUse(()=>i), ; i < 100; i++) ...
and in X10 you can't: you can't have multiple statements for the init clause.
Hide
Igor Peshansky added a comment -

Hmm, can you?

$ echo '#include <stdio.h>
int main(int ac, char** av) { for (int i = 1, j = 10; i < 10; i++, j--) printf(".\n"); }' \
| g++ -x c++ -
$ echo $?
0
$ echo '#include <stdio.h>
int main(int ac, char** av) { for (int i = 1, double j = 10; i < 10; i++, j--) printf(".\n"); }' \
| g++ -x c++ -
<stdin>: In function 'int main(int, char**)':
<stdin>:2: error: expected unqualified-id before 'double'
<stdin>:2: error: 'j' was not declared in this scope
$ echo $?
1
$ echo '#include <stdio.h>
int main(int ac, char** av) { double j = 100; for (int i = 1, j /= 10; i < 10; i++, j--) printf(".\n"); }' \
| g++ -x c++ -
<stdin>: In function 'int main(int, char**)':
<stdin>:2: error: expected initializer before '/=' token
$ echo $?
1
Show
Igor Peshansky added a comment - Hmm, can you?
$ echo '#include <stdio.h>
int main(int ac, char** av) { for (int i = 1, j = 10; i < 10; i++, j--) printf(".\n"); }' \
| g++ -x c++ -
$ echo $?
0
$ echo '#include <stdio.h>
int main(int ac, char** av) { for (int i = 1, double j = 10; i < 10; i++, j--) printf(".\n"); }' \
| g++ -x c++ -
<stdin>: In function 'int main(int, char**)':
<stdin>:2: error: expected unqualified-id before 'double'
<stdin>:2: error: 'j' was not declared in this scope
$ echo $?
1
$ echo '#include <stdio.h>
int main(int ac, char** av) { double j = 100; for (int i = 1, j /= 10; i < 10; i++, j--) printf(".\n"); }' \
| g++ -x c++ -
<stdin>: In function 'int main(int, char**)':
<stdin>:2: error: expected initializer before '/=' token
$ echo $?
1
Hide
Bard Bloom added a comment -

I am sufficiently archaic as to have only a pre-ANSI-C K&R. You can put expressions separated by commas in a for, even back then:
for (i=1, j=100; i<j; i++, j--)
But I guess that the tweak to allow declarations there only has one.

Well, that's really gorfy.

(Also, K&R say 'don't do this unless you really need to')

Anyhow, we can do the K&Rish

for (i=1,j=100; i < j; i++, j--)
	     Console.OUT.println(i+"->"+j);

So I guess that's probably good enough considering that (1) it's a stupid corner case, and (2) even C doesn't do it right if one were doing that corner case well.

Show
Bard Bloom added a comment - I am sufficiently archaic as to have only a pre-ANSI-C K&R. You can put expressions separated by commas in a for, even back then: for (i=1, j=100; i<j; i++, j--) But I guess that the tweak to allow declarations there only has one. Well, that's really gorfy. (Also, K&R say 'don't do this unless you really need to') Anyhow, we can do the K&Rish
for (i=1,j=100; i < j; i++, j--)
	     Console.OUT.println(i+"->"+j);
So I guess that's probably good enough considering that (1) it's a stupid corner case, and (2) even C doesn't do it right if one were doing that corner case well.
Hide
Bard Bloom added a comment -

Igor was right.

Show
Bard Bloom added a comment - Igor was right.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: