indexing a tuple containing a struct strange result

2013-06-23 Thread cal

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)
}


Re: indexing a tuple containing a struct strange result

2013-06-23 Thread Anthony Goins

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.


Re: indexing a tuple containing a struct strange result

2013-06-23 Thread Ali Çehreli

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



Re: indexing a tuple containing a struct strange result

2013-06-23 Thread Ali Çehreli

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



Re: indexing a tuple containing a struct strange result

2013-06-23 Thread cal

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!


Re: indexing a tuple containing a struct strange result

2013-06-24 Thread Ali Çehreli

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