convert ... to array

Qian Xu qian.xu at stud.tu-ilmenau.de
Tue Oct 20 05:58:17 PDT 2009


Chris Nicholson-Sauls wrote:

> 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

I have forgotten to say, that the class Foo comes from an external d-library
(tango), which means that I am not able to change the function interface.

I can only use the method foo.array(...)




More information about the Digitalmars-d-learn mailing list