Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-11 Thread Paul Backus via Digitalmars-d-learn

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


Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-11 Thread frame via Digitalmars-d-learn

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.


Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-11 Thread Matheus via Digitalmars-d-learn

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 hell


Well 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.


Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-11 Thread Matheus via Digitalmars-d-learn
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.


Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-10 Thread frame via Digitalmars-d-learn

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


Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-10 Thread Siarhei Siamashka via Digitalmars-d-learn

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.


Wouldn't the compiler be smart with this shadowing variable?

2021-12-10 Thread Matheus via Digitalmars-d-learn

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.