Thread overview | |||||
---|---|---|---|---|---|
|
December 07, 2010 unpacking | ||||
---|---|---|---|---|
| ||||
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 Re: unpacking | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | 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 Re: unpacking | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | 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.
|
Copyright © 1999-2021 by the D Language Foundation