www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - defect when using 'alias this' in a struct?

reply Kieron Brown <kieron_brown hotmail.com> writes:
I have used alias this and found odd behaviour in complex code.

I tracked the problem down and was able to replicate it
with a much simpler case as given below.

I'm new to D (but have read TDPL).
Is this my mis-understanding or a defect?

Many thanks, Kieron

----- self contained d sample starts -----
import std.stdio, std.math;

void main()
{
    auto p = Test(0.5, 0.5);
    assert(0.5 == p.a); // ok
    assert(0.5 == p.b); // ok
    Test q;
//  q = Test(0.5, 0.5);
    //
    // the above line gave same
    // error when subst for
    // the line below
    q = p;
    if (isNaN(q.b))
        printf("problem replicated\n");
    assert(0.5 == q.a); // ok
    assert(0.5 == q.b); // this fails
}

struct Test
{
    double a;
    double b;
    alias a this;
}
----- end of file -----
Aug 30 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 30 Aug 2010 13:12:00 -0400, Kieron Brown  
<kieron_brown hotmail.com> wrote:

 I have used alias this and found odd behaviour in complex code.

 I tracked the problem down and was able to replicate it
 with a much simpler case as given below.

 I'm new to D (but have read TDPL).
 Is this my mis-understanding or a defect?

 Many thanks, Kieron

 ----- self contained d sample starts -----
 import std.stdio, std.math;

 void main()
 {
     auto p = Test(0.5, 0.5);
     assert(0.5 == p.a); // ok
     assert(0.5 == p.b); // ok
     Test q;
 //  q = Test(0.5, 0.5);
     //
     // the above line gave same
     // error when subst for
     // the line below
     q = p;
     if (isNaN(q.b))
         printf("problem replicated\n");
     assert(0.5 == q.a); // ok
     assert(0.5 == q.b); // this fails
 }

 struct Test
 {
     double a;
     double b;
     alias a this;
 }
Yes, that's a bug. -Steve
Aug 30 2010