Thread overview
unpacking
Dec 07, 2010
spir
Dec 07, 2010
bearophile
Dec 08, 2010
Stewart Gordon
December 07, 2010
Hello D people,

Is there a way to unpack an array into local vars, as:
	auto x = [1,2,3];
	a,b,c = x;

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

December 07, 2010
spir:

> Is there a way to unpack an array into local vars, as:
> 	auto x = [1,2,3];
> 	a,b,c = x;

Not yet, but I have asked this feature for tuples too and maybe someone has appreciated it.

Bye,
bearophile
December 08, 2010
On 07/12/2010 17:29, spir wrote:
> Hello D people,
>
> Is there a way to unpack an array into local vars, as:
> 	auto x = [1,2,3];
> 	a,b,c = x;

import std.stdio;

void unpack(A, T...)(out T vars, A data) {
    assert (vars.length == data.length);
    foreach (i, v; vars) {
        vars[i] = data[i];
    }
}

void main() {
    auto x = [1,2,3];
    int a, b, c;
    unpack(a, b, c, x);
	
    writefln("%d %d %d", a, b, c);
}

Stewart.