Instance variables with @ (was: Re: why ; ?)
Nick Sabalausky
a at a.a
Fri May 9 13:20:30 PDT 2008
"Michael Neumann" <mneumann at ntecs.de> wrote in message
news:g01mtk$1nvj$1 at digitalmars.com...
> Nick Sabalausky wrote:
> > // Note: untested
> > class A {
> > int i=0;
> >
> > void foo() {
> > // Accidentially clobbers "this.i" aka "@i"
> > for(i=0; i<77; i++)
> > {/* Do stuff */}
> > }
> >
> > invariant() {
> > assert(this.i==0); // Fails after foo() is called
> > }
> > }
> >
> > I still like the @ thing, though.
>
> Very good example!
>
> A solution could be to force the programmer to use the "this." notation
> or at least issuing a compiler warning if not done. The latter could be
> implemented in the compiler without any changes to the syntax/language.
>
I had been thinking of this feature as a pipe dream (at least for D), just
because it would mean changing the language to always require "this." or
"@". Implimenting it as an optional warning hadn't occurred to me. That
makes it sound much more possible. Good call on that (no pun intended).
> The next step would be to have "@" as a synonym for "this.", as typing
> "this." all the time is either annoying and as such is ignored or leads
> to less readable code (IMHO).
>
Agreed. I would love to see this actually happen. Although, I do wonder if
maybe we're chasing too rare of a problem to bother, or maybe it would
segregate the D scene into "@D" people and "raw D" people (I hope not,
'cause I do like it).
Any comments from Walter? I'm curious what his take is on the original
problem.
More information about the Digitalmars-d
mailing list