Another interesting hack: expand a static array into parameter arguments

bearophile bearophileHUGS at lycos.com
Wed Apr 2 14:53:56 PDT 2014


Andrej Mitrovic:

> void foo(int a, int b, int c)
> {
>     writefln("a: %s, b: %s, c: %s", a, b, c);
> }
>
> void main()
> {
>     int[3] arr = [1, 2, 3];
>     foo(expand!arr);
> }
> -----

In Python2 and Haskell it's a built-in feature, as it should be:

>>> def foo((x, y, z)): print x, y, z
...
>>> foo([1, 2, 3])
1 2 3
>>> def bar(x, y, z): print x, y, z
...
>>> a = [1, 2, 3]
>>> bar(*a)
1 2 3


Prelude> let foo [x, y, z] = show x ++ ", " ++ show y ++ ", " ++ 
show z
Prelude> let a = [1, 2, 3]
Prelude> foo a
"1, 2, 3"

Bye,
bearophile


More information about the Digitalmars-d mailing list