On Tue, Jul 08, 2014 at 07:02:17PM +0000, NCrashed via Digitalmars-d-learn 
wrote:
[...]
> auto tie(StoreElements...)(ref StoreElements stores)
[...]

Here's my take on it, that doesn't need to use pointers:

        import std.typecons;
        
        template TypesOf(T...)
        {
                static if (T.length == 1)
                        alias TypesOf = typeof(T[0]);
                else
                        alias TypesOf = TypeTuple!(typeof(T[0]), 
TypesOf!(T[1..$]));
        }
        
        @property void tie(T...)(Tuple!(TypesOf!T) t)
        {
                foreach (i, ref var; T)
                {
                        T[i] = t[i];
                }
        }
        
        Tuple!(int, string) func() {
                return tuple(1, "a");
        }
        
        void main()
        {
                int x;
                string y;
        
                tie!(x,y) = func();
        
                import std.stdio;
                writefln("%d %s", x, y);
        }

It does involve some slightly more arcane template magic, though. ;-)
Basically, the TypesOf template transforms a list of alias arguments
into a list of the types of said arguments, so that we can match a list
of variables to the Tuple that is to be assigned to them.

This also (ab)uses the fact that assigning to a function call gets
interpreted in this context as setting a global @property, so there's no
need to overload opAssign at all. (Arguably, this makes it more an
insane hack than a clever solution!)


T

-- 
Once the bikeshed is up for painting, the rainbow won't suffice. -- Andrei 
Alexandrescu

Reply via email to