variadic func to call another variadic func

Chris Nicholson-Sauls ibisbasenji at gmail.com
Thu Aug 24 23:56:40 PDT 2006


Daniel919 wrote:
> Hi, I tried to create a simple solution for constructor inheritance
> (http://www.digitalmars.com/pnews/
> read.php?server=news.digitalmars.com&group=digitalmars.D.announce&artnum=4426)
> 
> The example code illustrates the problem, called in the topic.
> 
> --------------------------------------------------------------
> import std.string;
> import std.stdio;
> 
> class Base {
>      this() {
>          m_val = -1;
>      }
> 
>      this(int val) {
>          m_val = val;
>      }
> 
>      int m_val;
> }
> 
> class Derived : Base {
>      this(...) {
>          super(_arguments); // <= the problem
> 
>          // ... do some special Derived constructor stuff
>      }
> }
> 
> void main() {
>      Derived obj1 = new Derived();
>      writefln("obj1.m_val: " ~ format(obj1.m_val));
>      Derived obj2 = new Derived(12345);
>      writefln("obj2.m_val: " ~ format(obj2.m_val));
> }
> --------------------------------------------------------------
> 
> The constructor of Derived has to call the constructor of Base with
> the same parameters itself got called.
> 
> What would be the best way to do this ?

Currently the only way for variadic functions to pass their arguments forward is by making 
one's variadic functions dummy wrappers for workhorse functions taking the implicit 
variables as parameters... or in other words:

# class Foo {
#   this (...) { this(_arguments, _args); }
#   this (TypeInfo[] _arguments, void* _args) {
#     /* real work goes here */
#   }
# }

There have been numerous proposals in the past for providing a cleaner way of doing this, 
including my own idea of making '...' at the call site an operator for building variadic 
parameter lists.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-announce mailing list