convert ... to array

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Oct 20 05:01:44 PDT 2009


Qian Xu wrote:
> Hi All,
> 
> a function is declared as follows:
> 
>   class Foo
>   {
>     final Value array(...)
>     {
>       ...
>     }
>   }
> 
> 
> I can pass any number of parameters to this method array() like:
> 
>   auto foo = new Foo;
>   foo.array(1, 2, 3);
> 
> 
> But if I have only an array in hand, how to pass it to this method? Is it
> possible?
> 
>   int[] myarray = [1, 2, 3];
>   // how to pass "myarray" to foo.array(...)
> 
> 
> Best regards

If you only intend Foo.array() to accept params of a particular type, just an arbitrary 
number of them, there's a syntax that marries variadic arguments and arrays together:

class Foo {
     final Value array (int[] args ...) {
         ...
     }
}

This will allow any number of int's to be passed, which are quietly packaged as an int[], 
and also transparently accepts int[] as-is.  Obviously, though, it isn't any help if you 
need to accept various types, and I'm not sure how well std.variant plays with this.

-- Chris Nicholson-Sauls


More information about the Digitalmars-d-learn mailing list