www.digitalmars.com         C & C++   DMDScript  

D - Another type-safe variable argument list idea

Another one of my crazy ideas for type-safe variable argument functions. I
think this one is more practical. I only implemented char in this example
for simplicity:


char[] format(char[] f, this ...)
{
 uint ibefore, index = 0;
 char[] result;


 /*
  called first, must have no arguments; not necessary if no startup code
 */
 this()
 {
 }


 /*
  just a nested function
 */
 void findNext()
 {
  ibefore = index;

  while(index != f.length)
  {
   if(f[index] == '%')
   {
    index++;
    break;
   }

   index++;
  }
 }


 /*
  called when a char is passed as a parameter to the ...
 */
 this(char ch)
 {
  findNext();
  if(index == f.length || f[index] != 'c')
   throw new FormatException("Expected %c in format string");

  /*
   index - 1 to exclude the %c
  */
  result ~= f[ibefore .. index - 1] ~ (&ch)[0 .. 1];

  /*
   skip %c
  */
  index++;
 }


 /*
  called after matching all parameters; must return the type of the function
if not void
 */
 ~this()
 {
  findNext();
  if(index != f.length)
   throw new FormatException("Not enough parameters");

  return result ? result ~ f[ibefore .. f.length] : f;
 }
}


It would be used very similarly to printf/sprintf:
char[] s = format("hello %c world", 'D');
If a parameter is passed to it and the function doesn't have a constructor
for that type, it's a compile-time error! It would be up to the person
implementing the var arg function to allow, for example, a char to match
with a %d; or it could require the caller to cast it.
As for implementing this into the language, I think it could be setup like
an auto class with an overloaded operator.
Sep 01 2003