www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - indexing a tuple containing a struct strange result

reply "cal" <callumenator gmail.com> writes:
What is going on here?

import std.stdio, std.typecons;

struct S
{
     int x;
     Tuple!(S) foo() { return tuple(this); }
}

void main()
{
     S s;
     s.x = 8;
     writeln((s.foo()));     //output: Tuple!(S)(S(8))
     writeln((s.foo())[0]);  //output: S(0)
}
Jun 23 2013
parent reply "Anthony Goins" <neontotem gmail.com> writes:
On Monday, 24 June 2013 at 01:22:12 UTC, cal wrote:
 What is going on here?

 import std.stdio, std.typecons;

 struct S
 {
     int x;
     Tuple!(S) foo() { return tuple(this); }
 }

 void main()
 {
     S s;
     s.x = 8;
     writeln((s.foo()));     //output: Tuple!(S)(S(8))
     writeln((s.foo())[0]);  //output: S(0)
 }
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 would explain what's going on but I'd be wrong.
Jun 23 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 23 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/23/2013 10:07 PM, Ali Çehreli wrote:

 I think it is a compiler bug.
Make that a Phobos bug. :) The following is a reduced program that exhibits the problem. The presence or absence of the unused member function makes a difference: import std.typecons; struct S { int x; // Bizarre: Comment-out this function to pass the assert in main. Tuple!(S) unused() { return tuple(S(7)); } } void main() { auto s = S(8); assert(tuple(s).expand[0] == S(8)); } Ali
Jun 23 2013
parent reply "cal" <callumenator gmail.com> writes:
On Monday, 24 June 2013 at 05:31:29 UTC, Ali Çehreli wrote:
 On 06/23/2013 10:07 PM, Ali Çehreli wrote:

 I think it is a compiler bug.
Make that a Phobos bug. :) The following is a reduced program that exhibits the problem. The presence or absence of the unused member function makes a difference: import std.typecons; struct S { int x; // Bizarre: Comment-out this function to pass the assert in main. Tuple!(S) unused() { return tuple(S(7)); } } void main() { auto s = S(8); assert(tuple(s).expand[0] == S(8)); } Ali
Actually I hadn't tried with free functions, but this test captures my problem. I'll file it now. Thanks!
Jun 23 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/23/2013 11:11 PM, cal wrote:

 I'll file it now. Thanks!
Thanks for filing: http://d.puremagic.com/issues/show_bug.cgi?id=10458 Ali
Jun 24 2013