daokoder / dao

Dao Programming Language
http://daoscript.org
Other
198 stars 19 forks source link

Bug: ++ operator issues in a for loop #511

Closed dumblob closed 9 years ago

dumblob commented 9 years ago

The last two keyboard interrupts are due to infinite looping.

0$ dao -e 'x = 5; io.writeln(++x)'
6
= none
0$ dao -e 'x = 5; io.writeln(++(int)x)'
6
= none
0$ dao -e 'x = 5; io.writeln(++((int)x))'
6
= none
0$ dao -e 'for (x = 5; x < 10; ++x) {}'
= none
0$ dao -e 'for (x = 5; x < 10; ++(int)x) {}'
keyboard interrupt...
1$ dao -e 'for (x = 5; x < 10; ++((int)x)) {}'
keyboard interrupt...
Night-walker commented 9 years ago

(int)x gives you a copy of x, so it's natural that modifying it has no effect on the original variable.

dumblob commented 9 years ago

Well, I don't feel it naturally - especially if considering cases with casting any variables (in such cases, there is absolutely no assumption, that the content of the variable will be copied) or variables of the same type as the casted type.

0$ dao -e 'for (x: any = 5; x < 10; ++(int)x) {}'
keyboard interrupt...
Night-walker commented 9 years ago

Casting a value of primitive type yields its copy, just as with assigning or passing to a routine. It is weird to expect a reference here. It doesn't matter whether it is any or the same type -- casting always yields an rvalue, in any sane language.

dumblob commented 9 years ago

In general, copying of primitive types makes sense - it's just a bit unfortunate with the ++ operator -- yes, old C habits die hard, because C forbids this:

0$ gcc -pedantic -std=c99 del/test.c
del/test.c: In function ‘main’:
del/test.c:8:29: error: lvalue required as increment operand
   printf("%d, %d, %f\n", x, ++(int)x, ++(int)x);
                             ^
del/test.c:8:39: error: lvalue required as increment operand
   printf("%d, %d, %f\n", x, ++(int)x, ++(int)x);
                                       ^
0$ gcc -pedantic -std=c99 del/test.c
del/test.c: In function ‘main’:
del/test.c:9:29: error: lvalue required as increment operand
   printf("%d, %d, %f\n", x, ++((int)x), ++((int)x));
                             ^
del/test.c:9:41: error: lvalue required as increment operand
   printf("%d, %d, %f\n", x, ++((int)x), ++((int)x));
                                         ^

What about forbidding it as well (there is absolutely no sense in such construction - it's just a source of mistakes)?

Night-walker commented 9 years ago

Of course it is better to forbid or warn about meaningless modifications of data.

daokoder commented 9 years ago

Of course it is better to forbid or warn about meaningless modifications of data.

Done.