non-typesafe variadic lazy arguments

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 28 11:19:31 PST 2008


I'm sure this has been asked before, but I can't remember the answer.

How does one create a function that takes non-typed variadic arguments?

Essentially, what I want is for this to work:

void f(lazy ...);

The current spec allows for typed variadic lazy functions i.e.:

void f(char[] delegate()[] dg ...); // every arg has to be a char[]

But if I want any type to be passed in, there doesn't seem to be a way to do 
it, as there is no builtin type that any type can be casted to implicitly 
(or is there?).

If I want to evaluate the variadic args lazily, I can use a level of 
indirection:

char[] evalOnCondition(bool condition, lazy char[] result)
{
   if(condition) writefln(result);
}

variadicF(char[] buffer, ...){...}

char[400] buf;
evalOnCondition(loops==5, variadicF(buf, "inloop", 5));

But what I really want to do is intercept the variadic function call.  I 
want to provide a function that allows you to call evalOnCondition and 
variadicF without a buffer and without allocating it from the heap:

variadicEvalOnCondition(bool condition, lazy ...)
{
  char[400] buf;
  if(condition)
    writefln(variadicF(buf, _argptr, _arguments)); // doesn't eval ... until 
here
}

Any ideas how this can be done?

-Steve 




More information about the Digitalmars-d-learn mailing list