digitalmars.D.learn - Wouldn't the compiler be smart with this shadowing variable?
- Matheus (21/21) Dec 10 2021 Hi,
- Siarhei Siamashka (10/22) Dec 10 2021 Intuitively, there are two possible interpretations here for
- Matheus (7/13) Dec 11 2021 Yes.
- frame (7/16) Dec 10 2021 You probably want this:
- Matheus (5/12) Dec 11 2021 Well this works! :)
- frame (14/17) Dec 11 2021 Besides it looks like an error, you may think that it just
- Paul Backus (7/15) Dec 11 2021 That's because `{++i; ++j;}` in an expression context is
Hi, Wouldn't the compiler be smart with this shadowing variable, example: void main(){ int j; for(int i=0,j=0;i<10;++i){} return; } onlineapp.d(3): Error: variable `j` is shadowing variable `onlineapp.main.j` So in the "for loop" shouldn't "i" be declared and "j" just be assigned to 0? I mean this is a bit weird, imagine I have different "for loops" and in this situation I'll need to do this: j=0; for(int i=0;i<10;++i){} Otherwise: for(i=0,j=0;i<10;++i){} This works, but now "i" variable will be out of the "for loop" scope. Matheus.
Dec 10 2021
On Friday, 10 December 2021 at 13:35:37 UTC, Matheus wrote:Hi, Wouldn't the compiler be smart with this shadowing variable, example: void main(){ int j; for(int i=0,j=0;i<10;++i){} return; } onlineapp.d(3): Error: variable `j` is shadowing variable `onlineapp.main.j` So in the "for loop" shouldn't "i" be declared and "j" just be assigned to 0?Intuitively, there are two possible interpretations here for inexperienced humans (and you seem to be favoring the latter): 1. declare a new "j" variable to be visible only inside of the loop. 2. reuse the existing "j" variable. But only one of them is a correct description of what actually happens when the compiler processes this code. So it's a good thing that the compiler is smart enough to reject ambiguous code and prevent humans from making mistakes.
Dec 10 2021
On Friday, 10 December 2021 at 21:55:17 UTC, Siarhei Siamashka wrote:... 2. reuse the existing "j" variable.Yes.But only one of them is a correct description of what actually happens when the compiler processes this code. So it's a good thing that the compiler is smart enough to reject ambiguous code and prevent humans from making mistakes.Hmm I see your point. And I just thought in cases like this the reuse should be the way, but I can see that this is a small snippet and in a big code this may hurt someone. Matheus.
Dec 11 2021
On Friday, 10 December 2021 at 13:35:37 UTC, Matheus wrote:I mean this is a bit weird, imagine I have different "for loops" and in this situation I'll need to do this: j=0; for(int i=0;i<10;++i){} Otherwise: for(i=0,j=0;i<10;++i){} This works, but now "i" variable will be out of the "for loop" scope. Matheus.You probably want this: ```d int j; for({int i=0; j=0;} i<10; ++i){} ``` Beware, this syntax comes directly from hell
Dec 10 2021
On Saturday, 11 December 2021 at 01:02:36 UTC, frame wrote:... You probably want this: ```d int j; for({int i=0; j=0;} i<10; ++i){} ``` Beware, this syntax comes directly from hellWell this works! :) I'm just a bit intrigued by your last sentence. Is there anything evil this may result or anything that I should be aware of? Matheus.
Dec 11 2021
On Saturday, 11 December 2021 at 15:15:06 UTC, Matheus wrote:I'm just a bit intrigued by your last sentence. Is there anything evil this may result or anything that I should be aware of?Besides it looks like an error, you may think that it just introduces a scope which can be referenced by the for-loop but this only works in the initialization section. See what happens if you want to do more steps at iteration for this fancy looking syntax: ```d // iteration works, but scope behind does nothing for({int i=0; j=0;} i<10; ++i, {++i; ++j;} ){} // endless loop for({int i=0; j=0;} i<10; {++i; ++j;} ){} ``` Not sure if bug or feature but it is inconsistent for me and thus a reason to avoid it.
Dec 11 2021
On Saturday, 11 December 2021 at 20:22:13 UTC, frame wrote:```d // iteration works, but scope behind does nothing for({int i=0; j=0;} i<10; ++i, {++i; ++j;} ){} // endless loop for({int i=0; j=0;} i<10; {++i; ++j;} ){} ``` Not sure if bug or feature but it is inconsistent for me and thus a reason to avoid it.That's because `{++i; ++j;}` in an expression context is shorthand syntax for `delegate void () {++i; ++j;}`. If you look at the [grammar for the `for` statement][1], you'll see that the "initialize" part allows a *statement*, but the "test" and "increment" parts only allow *expressions*. [1]: https://dlang.org/spec/statement.html#ForStatement
Dec 11 2021