| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
June 24, 2013 indexing a tuple containing a struct strange result | ||||
|---|---|---|---|---|
| ||||
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)
}
| ||||
June 24, 2013 Re: indexing a tuple containing a struct strange result | ||||
|---|---|---|---|---|
| ||||
Posted in reply to cal | 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.
| |||
June 24, 2013 Re: indexing a tuple containing a struct strange result | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Anthony Goins | 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
| |||
June 24, 2013 Re: indexing a tuple containing a struct strange result | ||||
|---|---|---|---|---|
| ||||
Posted in reply to 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
| |||
June 24, 2013 Re: indexing a tuple containing a struct strange result | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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!
| |||
June 24, 2013 Re: indexing a tuple containing a struct strange result | ||||
|---|---|---|---|---|
| ||||
Posted in reply to cal | 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 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply