Thread overview | |||||
---|---|---|---|---|---|
|
May 01, 2017 Using the same name for diffrent entities | ||||
---|---|---|---|---|
| ||||
Hello, Is it possible to use the same name for different entities? For example calling a variable and a class\function by the same name. |
May 01, 2017 Re: Using the same name for diffrent entities | ||||
---|---|---|---|---|
| ||||
Posted in reply to Noy | On Monday, 1 May 2017 at 14:01:19 UTC, Noy wrote:
> Hello,
> Is it possible to use the same name for different entities? For example calling a variable and a class\function by the same name.
Symbols declared in the same scope must be unique. For example:
```
module foo;
int bar;
void bar();
class bar {}
```
This will not compile as the three symbols conflict. But:
```
module foo1;
int bar;
module foo2;
void bar();
module foo3;
import foo1, foo2;
class bar {}
```
This is fine. You can disambiguate using fully qualified names: `bar` will refer to the class, foo1.bar to the variable, and foo2.bar to the function.
You can shadow symbols in inner scopes:
```
class bar {}
void main() {
void bar() {}
void foo() { int bar; }
}
```
That would be really confusing, though.
|
May 01, 2017 Re: Using the same name for diffrent entities | ||||
---|---|---|---|---|
| ||||
Posted in reply to Noy | On Monday, 1 May 2017 at 14:01:19 UTC, Noy wrote:
> Hello,
> Is it possible to use the same name for different entities? For example calling a variable and a class\function by the same name.
It only works with functions that take different parameters or with templated functions that have constraints, aka "overloads", because the compiler can determine what you want to call.
|
Copyright © 1999-2021 by the D Language Foundation