On Tuesday, 8 July 2014 at 17:42:00 UTC, Remo wrote:

How to make something that work like std::tie in D2 ?

Tuple!(float, float) sinCos(float n) {
return tuple(cast(float)sin(n), cast(float)cos(n)); //please note cast(float)!
}

int main(string[] argv) {
 float s,c;
 tie!(s,c) = sinCos(3.0f);
}

Try the following approach:
```
import std.typecons;
import std.typetuple;
import std.math;
import std.stdio;

auto tie(StoreElements...)(ref StoreElements stores)
{
        alias Elements = staticMap!(Unqual, StoreElements);
        
        template toPointer(T)
        {
                alias toPointer = T*;
        }
        
        struct Holder
        {
                alias StoreElementsPtrs = staticMap!(toPointer, StoreElements);
                StoreElementsPtrs storePtrs;
                
                this(ref StoreElements stores)
                {
                        foreach(i, _; StoreElements)
                        {
                                storePtrs[i] = &stores[i];
                        }
                }
                
                void opAssign(Tuple!Elements values)
                {
                        foreach(i, _; Elements)
                        {
                                *storePtrs[i] = values[i];
                        }
                }
        }
        
        return Holder(stores);
}

Tuple!(float, float) sinCos(float n)
{
return tuple(cast(float)sin(n), cast(float)cos(n)); //please note cast(float)!
}

void main()
{
        float s,c;
        tie(s,c) = sinCos(3.0f);
        writeln(s, " ", c);
}
```

Reply via email to