Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 16, 2011 Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
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 |
December 16, 2011 Re: Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | I think something like this is implemented in a dmd pull request. |
December 16, 2011 Re: Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | 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. |
December 16, 2011 Re: Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | 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 |
December 16, 2011 Re: Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | > 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
|
December 17, 2011 Re: Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | 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.
|
December 17, 2011 Re: Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | 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.
|
December 17, 2011 Re: Alias/Ref Tuples ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | On Sat, Dec 17, 2011 at 01:23, Simen Kjærås <simen.kjaras@gmail.com> wrote:
> 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.
Wow. How does that work? I'd understand:
TypeTuple!(a,b) = tuple(a,b).expand; // Or .tupleof, even.
but not your example... Does that mean TypeTuple!() = does some destructuring?
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 = 1;
b = 2;
// TypeTuple!(a, b) = Pair1(b, a+b); // boom!
TypeTuple!(a, b) = Pair2(b, a+b); // works!
writeln(a," ",b);
}
So it's a side effect of alias this and tuples...
|
Copyright © 1999-2021 by the D Language Foundation