Thread overview
Re: Behaviour of alias this changed
May 08, 2012
Gor Gyolchanyan
May 08, 2012
H. S. Teoh
May 08, 2012
Jonathan M Davis
May 08, 2012
H. S. Teoh
May 08, 2012
Jonathan M Davis
May 09, 2012
kenji hara
May 10, 2012
H. S. Teoh
May 08, 2012
My guess is - it's a bug, because alias this is supposed to be always less prioritized, then actual members.

On Tue, May 8, 2012 at 9:11 PM, H. S. Teoh <hsteoh@quickfur.ath.cx> 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()?
>
>
> T
>
> --
> To err is human; to forgive is not our policy. -- Samuel Adler



-- 
Bye,
Gor Gyolchanyan.
May 08, 2012
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.


T

-- 
Today's society is one of specialization: as you grow, you learn more and more about less and less. Eventually, you know everything about nothing.
May 08, 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
May 08, 2012
On Tue, May 08, 2012 at 01:52:14PM -0400, Jonathan M Davis wrote:
> 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));
[...]
> > 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.
[...]

Yes, it would be nice to finally make toString const @safe pure and nothrow.

But the question still stands: what to do with the different behaviour of alias this in git dmd?


T

-- 
Famous last words: I wonder what will happen if I do *this*...
May 08, 2012
On Tuesday, May 08, 2012 10:57:20 H. S. Teoh wrote:
> On Tue, May 08, 2012 at 01:52:14PM -0400, Jonathan M Davis wrote:
> > 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));
> 
> [...]
> 
> > > 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.
> 
> [...]
> 
> Yes, it would be nice to finally make toString const @safe pure and nothrow.
> 
> But the question still stands: what to do with the different behaviour of alias this in git dmd?

I'd argue that what you're seeing is a bug, but it may be due in part to a partial transition to requiring toString to be const @safe pure nothrow.

- Jonathan M Davis
May 09, 2012
2012/5/9 H. S. Teoh <hsteoh@quickfur.ath.cx>:
> 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.

This is a Phobos regression in git head (and maybe in 2.059).
Please file this in bugzilla.

Kenji Hara

----
import std.traits;

struct S {
    short[4] data;
    alias data this;
    string toString() { return "<S>"; }
}
void main()
{
    S s;
    static assert(isStaticArray!S); // succeeds in 2.059, failed in
2.058 and earlier
    toImpl!string(s);  // matches to static array version
}

extern(C) int printf(const char*, ...);
void toImpl(T, S)(S s)
    if (is(S == struct) && is(typeof(&S.init.toString)) &&
        isSomeString!T)
{
    printf("1:\n");
}
void toImpl(T, S)(ref S s)
    if (isStaticArray!S)
{
    printf("2:\n");
}
May 10, 2012
On Wed, May 09, 2012 at 02:10:37PM +0900, kenji hara wrote:
> 2012/5/9 H. S. Teoh <hsteoh@quickfur.ath.cx>:
> > 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.
> 
> This is a Phobos regression in git head (and maybe in 2.059).
> Please file this in bugzilla.
[...]

Done, sorry for the delay (too many forum posts to catch up with :-P):

	http://d.puremagic.com/issues/show_bug.cgi?id=8080


T

-- 
Why have vacation when you can work?? -- EC