Behaviour of alias this changed
Jonathan M Davis
jmdavisProg at gmx.com
Tue May 8 10:52:14 PDT 2012
On Tuesday, May 08, 2012 10:30:53 H. S. Teoh wrote:
> On Tue, May 08, 2012 at 10:15:00AM -0700, Ali Çehreli wrote:
> > On 05/08/2012 10:11 AM, H. S. Teoh wrote:
> > >I have some code along these lines:
> > > struct S {
> > >
> > > short[4] data;
> > > alias this data;
> > >
> > > string toString() { ... }
> > >
> > > }
> > > ...
> > > S s;
> > > writeln(to!string(s));
> > >
> > >In dmd 2.059 (I believe) and earlier, this calls S.toString(). However,
> > >in git dmd, this calls data.toString() instead.
> > >
> > >I'm just curious about the rationale for this change, and whether
> > >there's a way to override the toString() call so that it always calls
> > >S.toString()?
>
> [...]
>
> > Just a guess: It could be related to const-correctness improvements.
> > Try defining S.toString() as const.
>
> [...]
>
> I tried that, it didn't help. Declaring string toString() const {...}
> still has data.toString being called instead of S.toString.
There's also @safe, pure, and nothrow. As I understand it, toString will
eventually need to have all 4 of those attributes. pure doesn't work very well
for toString yet though, since most of the string-related conversion functions
can't be pure yet due to impure low level constructs that they use.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list