indexing a tuple containing a struct strange result

Ali Çehreli acehreli at yahoo.com
Sun Jun 23 22:07:07 PDT 2013


On 06/23/2013 09:40 PM, Anthony Goins wrote:

 > On Monday, 24 June 2013 at 01:22:12 UTC, cal wrote:

 >>     Tuple!(S) foo() { return tuple(this); }

 > import std.stdio, std.typecons;
 >
 > struct S
 > {
 >      int x;
 >      int y;
 >      int z;
 >
 >      auto foo() { return tuple(this.tupleof); }
 > }
 >
 > void main()
 > {
 >      S s;
 >      s.x = 8;
 >      s.y = 9;
 >      s.z = 10;
 >      writeln((s.foo()));     //output: Tuple!(int, int, int)(8, 9, 10)
 >
 >      writeln(s.foo()[2]);  //output: 10
 > }
 >
 > Is this what you expected?

I think the OP is asking about the difference from when foo() is a 
non-member function:

import std.stdio, std.typecons;

struct S
{
     int x;
}

Tuple!(S) foo(S s)
{
     return tuple(s);
}

void main()
{
     S s;
     s.x = 8;
     writeln((s.foo()));     //output: Tuple!(S)(S(8))
     writeln((s.foo())[0]);  //output: S(8)
}

This time the output is S(8).

I think it is a compiler bug.

Ali



More information about the Digitalmars-d-learn mailing list