Constructor Parameter vs Member Variable

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Dec 5 17:56:31 UTC 2023


On Tuesday, December 5, 2023 10:45:27 AM MST DrDread via Digitalmars-d wrote:
> On Tuesday, 5 December 2023 at 16:30:42 UTC, Jonathan M Davis
>
> wrote:
> > On Tuesday, December 5, 2023 7:23:26 AM MST Christopher Winter
> >
> > via Digitalmars-d wrote:
> >> [...]
> >
> > A big difference is that if you shadow a local variable with
> > another local variable, you don't have any way to differentiate
> > between them. So, when you shadow a local variable, that
> > variable becomes inaccessible within that section of code,
> > whereas with a member variable, you can distinguish with the
> > this reference, and with a variable at module scope, you can
> > differentiate a leading dot. So, the error prevents you from
> > making variables inaccessible, whereas there is no need for
> > such an error when shadowing non-local variables.
> >
> > [...]
>
> a better option would be to force the use of this. to access
> member variables instead of implicitly making them part of the
> scope. but that ship has sailed a long time ago

There are indeed some languages that do that, but a lot of us would find
such a requirement to be extremely annoying, especially since in practice,
problems with shadowing member variables are quite rare.

- Jonathan M Davis





More information about the Digitalmars-d mailing list