www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - convert ... to array

reply Qian Xu <qian.xu stud.tu-ilmenau.de> writes:
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
Oct 20 2009
next sibling parent Zarathustra <adam.chrapkowski gmail.com> writes:
  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);
Oct 20 2009
prev sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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
Oct 20 2009
parent reply Qian Xu <qian.xu stud.tu-ilmenau.de> writes:
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(...)
Oct 20 2009
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Oct 20 2009