| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
  | 
October 20, 2009 convert ... to array | ||||
|---|---|---|---|---|
  | ||||
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
 | ||||
October 20, 2009 Re: convert ... to array | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Qian Xu  |   import std.stdio;
  class Foo{
    final int array(...){
      for(uint i = 0; i < _arguments.length; i++){
        if(_arguments[i] == typeid(int [])){
          int [] l_arr = *cast(int []*)_argptr;
          writefln("%d", l_arr[0]);
        }
      }
      return 0x0;
    }
  }
  //
    Foo foo = new Foo;
    int [] myarray = [1, 2, 3];
    foo.array(myarray);
 | |||
October 20, 2009 Re: convert ... to array | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Qian Xu  | 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
 | |||
October 20, 2009 Re: convert ... to array | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Chris Nicholson-Sauls  | 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(...)
 | |||
October 20, 2009 Re: convert ... to array | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Qian Xu  | On Tue, 20 Oct 2009 08:58:17 -0400, Qian Xu <qian.xu@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
 | |||
Copyright © 1999-2021 by the D Language Foundation
 
Permalink
Reply