convert ... to array

Steven Schveighoffer schveiguy at yahoo.com
Tue Oct 20 11:02:35 PDT 2009


On Tue, 20 Oct 2009 08:58:17 -0400, Qian Xu <qian.xu at stud.tu-ilmenau.de>  
wrote:

> 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(...)

typically, tango calls a non-variadic version of a variadic function with  
the array and type array.  You can see if there is a non-variadic version  
to call instead.

However, it would be a nice feature to be able to signify you want to  
package the args yourself, rather than having to resort to this kind of  
stuff.  I'm sure a library solution is possible.

-Steve


More information about the Digitalmars-d-learn mailing list