digitalmars.D.learn - indexing a tuple containing a struct strange result
- cal (14/14) Jun 23 2013 What is going on here?
- Anthony Goins (21/35) Jun 23 2013 import std.stdio, std.typecons;
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (22/42) Jun 23 2013 I think the OP is asking about the difference from when foo() is a
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (20/21) Jun 23 2013 Make that a Phobos bug. :)
- cal (3/26) Jun 23 2013 Actually I hadn't tried with free functions, but this test
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/5) Jun 24 2013 Thanks for filing:
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
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
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
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
On Monday, 24 June 2013 at 05:31:29 UTC, Ali Çehreli wrote:On 06/23/2013 10:07 PM, Ali Çehreli wrote:Actually I hadn't tried with free functions, but this test captures my problem. I'll file it now. Thanks!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
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