www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Alias/Ref Tuples ?

reply Joshua Reusch <yoschi arkandos.de> writes:
Hello,

is there a way to say something like

---
int a, b;
AliasTuple!(a, b) = tuple(4,5);
assert(a == 4 && b == 5);
---

without having to write an own AliasTuple template ? I want to use it 
for functions returning multiple values.

Joshua Reusch
Dec 16 2011
next sibling parent Trass3r <un known.com> writes:
I think something like this is implemented in a dmd pull request.
Dec 16 2011
prev sibling next sibling parent Joshua Reusch <yoschi arkandos.de> writes:
I found a way doing this with a simple function:

---
void explode(R, T...)(R range, ref T values) {
	static if(hasLength!R) assert(range.length == T.length);
	foreach(i, value; range) values[i] = value;
}
---

but a more self-documenting version would be nice.
Dec 16 2011
prev sibling next sibling parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Fri, 16 Dec 2011 14:00:11 +0100, Joshua Reusch <yoschi arkandos.de>  
wrote:

 Hello,

 is there a way to say something like

 ---
 int a, b;
 AliasTuple!(a, b) = tuple(4,5);
 assert(a == 4 && b == 5);
 ---

 without having to write an own AliasTuple template ? I want to use it  
 for functions returning multiple values.

There is one in dranges: http://dsource.org/projects/dranges It is not officially documented, and I don't know how good it actually is, but here's what documentation exists: http://svn.dsource.org/projects/dranges/trunk/dranges/docs/reftuple.html
Dec 16 2011
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
 There is one in dranges:

 http://dsource.org/projects/dranges

 It is not officially documented, and I don't know how good it actually is,
 but here's what documentation exists:

 http://svn.dsource.org/projects/dranges/trunk/dranges/docs/reftuple.html

Hmm, thanks Simen, but no. It was a simple hack I did in 10' one day to play with pointers without knowing what I was doing. I wouldn't use it if I were you, it's quite unsafe. I'm following with *great* interest all the nice changes in DMD that Kanji is adding. We may have a nice tuple syntax in 2012, who knows? Gosh, there should be a way to get extensions into DMD, like the Glasgow Haskell Compiler. Like: pragma(extension, tupleExpansionSyntax); // thanks, Kanji! Philippe
Dec 16 2011
prev sibling next sibling parent reply =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Fri, 16 Dec 2011 14:00:11 +0100, Joshua Reusch <yoschi arkandos.de>  
wrote:

 Hello,

 is there a way to say something like

 ---
 int a, b;
 AliasTuple!(a, b) = tuple(4,5);
 assert(a == 4 && b == 5);
 ---

 without having to write an own AliasTuple template ? I want to use it  
 for functions returning multiple values.

A few small tests later: import std.typetuple; import std.typecons; import std.stdio; void main() { int a, b; TypeTuple!(a, b) = tuple(4,5); assert(a == 4 && b == 5); } In other words, the language already has this. Note that TypeTuple!(a,b) = TypeTuple!(b,a) also works, but sets both a and b equal to b.
Dec 16 2011
parent Joshua Reusch <yoschi arkandos.de> writes:
Am 17.12.2011 01:23, schrieb Simen Kjærås:
 On Fri, 16 Dec 2011 14:00:11 +0100, Joshua Reusch <yoschi arkandos.de>
 wrote:

 Hello,

 is there a way to say something like

 ---
 int a, b;
 AliasTuple!(a, b) = tuple(4,5);
 assert(a == 4 && b == 5);
 ---

 without having to write an own AliasTuple template ? I want to use it
 for functions returning multiple values.

A few small tests later: import std.typetuple; import std.typecons; import std.stdio; void main() { int a, b; TypeTuple!(a, b) = tuple(4,5); assert(a == 4 && b == 5); } In other words, the language already has this. Note that TypeTuple!(a,b) = TypeTuple!(b,a) also works, but sets both a and b equal to b.

Thank you ! This is exactly what I needed ! I didnt thought TypeTuple can do this.
Dec 16 2011
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sat, Dec 17, 2011 at 01:23, Simen Kj=C3=A6r=C3=A5s <simen.kjaras gmail.c=
om> wrote:
 A few small tests later:

 import std.typetuple;
 import std.typecons;
 import std.stdio;

 void main() {
 =C2=A0 =C2=A0int a, b;
 =C2=A0 =C2=A0TypeTuple!(a, b) =3D tuple(4,5);

 =C2=A0 =C2=A0assert(a =3D=3D 4 && b =3D=3D 5);
 }

 In other words, the language already has this.

Wow. How does that work? I'd understand: TypeTuple!(a,b) =3D tuple(a,b).expand; // Or .tupleof, even. but not your example... Does that mean TypeTuple!() =3D does some destructu= ring? Let's do some test: struct Pair1 { TypeTuple!(int,int) value; } struct Pair2 { TypeTuple!(int,int) value; alias value this;} void main() { int a, b; a =3D 1; b =3D 2; // TypeTuple!(a, b) =3D Pair1(b, a+b); // boom! TypeTuple!(a, b) =3D Pair2(b, a+b); // works! writeln(a," ",b); } So it's a side effect of alias this and tuples...
Dec 17 2011