Constructor Parameter vs Member Variable

Christopher Winter christopher.winter at ahrefs.com
Tue Dec 5 14:23:26 UTC 2023


On Tuesday, 5 December 2023 at 04:31:28 UTC, Jonathan M Davis 
wrote:
> This behavior is actually extremely common in OO programming 
> languages.

Yeah, I understand that it is variable shadowing, but this nearly 
identical situation is rejected by the compiler:

```
void fun(double a)
{
     int a = cast(int) a; <-- Error: variable `a` is shadowing 
variable `main.fun.a`
}
```

I don't really understand why these scenarios would be treated 
differently by the compiler (I would even argue that my intention 
here is more explicit vs in the constructor, but not everyone may 
agree with that).

> The solution to this is to use the this reference when there's 
> any ambiguity.

Yeah, I suppose for me always using `this` is probably the best 
way to avoid any ambiguity.


More information about the Digitalmars-d mailing list