Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 21, 2021 Foo Foo = new Foo(); | ||||
---|---|---|---|---|
| ||||
class Foo { } void main() { Foo Foo = new Foo(); } this kind of code compiles. Is this expected to compile? |
February 21, 2021 Re: Foo Foo = new Foo(); | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Sunday, 21 February 2021 at 18:07:49 UTC, JN wrote:
> class Foo
> {
> }
>
> void main()
> {
> Foo Foo = new Foo();
> }
>
> this kind of code compiles. Is this expected to compile?
Yes, why wouldn't it? main is a different scope than global; you can override identifiers from global in main. And "Foo" only exists after the declaration, so it doesn't conflict.
|
February 21, 2021 Re: Foo Foo = new Foo(); | ||||
---|---|---|---|---|
| ||||
Posted in reply to FeepingCreature | On Sunday, 21 February 2021 at 18:09:29 UTC, FeepingCreature wrote:
> On Sunday, 21 February 2021 at 18:07:49 UTC, JN wrote:
>> class Foo
>> {
>> }
>>
>> void main()
>> {
>> Foo Foo = new Foo();
>> }
>>
>> this kind of code compiles. Is this expected to compile?
>
> Yes, why wouldn't it? main is a different scope than global; you can override identifiers from global in main. And "Foo" only exists after the declaration, so it doesn't conflict.
I was worried I hit some corner case where it compiles even though it shouldn't. Can lead to some confusing code. I guess D is smart enough to figure out which is a type and which is a variable. C++ gets confused in similar situation.
|
February 21, 2021 Re: Foo Foo = new Foo(); | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Sunday, 21 February 2021 at 18:15:22 UTC, JN wrote:
> I guess D is smart enough to figure out which is a type and which is a variable. C++ gets confused in similar situation.
Well it isn't about type vs variable (except for in this exact declaration), it is just one is defined at top level and the other is defined local to the function. You can have local variables with the name name as top-level variables.
If you had another local thing called Foo already, even if it was a type and not a variable, then it would complain.
|
February 21, 2021 Re: Foo Foo = new Foo(); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Why doesn't this compiles? class Baa { Foo Foo = new Foo(); } |
February 21, 2021 Re: Foo Foo = new Foo(); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | On Sunday, 21 February 2021 at 21:03:27 UTC, Jack wrote:
> Why doesn't this compiles?
>
> class Baa
> {
> Foo Foo = new Foo();
> }
Local variables inside functions are a little bit special because everything happens in sequence. This is a declaration, where there is a new namespace, but order doesn't matter.
So in the function:
void foo() {
// there is no local variable Foo yet until after this line,
// so there is no ambiguity - only existing Foo here is the class outside
Foo Foo = new Foo();
// Foo now refers to the local variable
// same as if you did
x++; // error: x doesn't exist yet
int x; // only AFTER this point does x exist
}
But in the class:
class foo {
// the names in here are all created simultaneously
// and thus the local Foo is considered existing as it
// looks up the name Foo
Foo Foo = new Foo();
// same as if you did
int y = x; // error is "Variable x not readable at compile time", NOT "no such variable" because all declarations flash into existence simultaneously
int x; // this line and the above could be swapped without changing anything
}
So the local variable isn't special just because of the namespace, it is also special because order matters inside functions but not outside.
|
Copyright © 1999-2021 by the D Language Foundation