www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variadic function idea...

reply Regan Heath <regan netwin.co.nz> writes:
Is there a way to avoid runtime type checking? For example say I have a 
variadic function that will only accept certain types eg. int, and uint 
currently I have to go something like..

void fn(...) {
   if (_arguments[i] != typeid(int) &&
       _arguments[i] != typeid(uint)) {
     //error
   }
   //etc
}

which tests at runtime and throws an error.

Instead it'd be nice if I could go

void fn(...) {
   _argtypes[] = [int,uint];
   //etc
}

or

void fn(...)
argtypes(int,uint) {
}
body {
   //etc
}

and at compile time it could check the types being passed to this fn and 
generate an error.

Regan.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 24 2004
parent reply "Walter" <newshound digitalmars.com> writes:
I think what you're asking for is ably handled by using an 'in' contract to
test the parameters. An advanced compiler could conceivably test the 'in'
contracts at compile time.

"Regan Heath" <regan netwin.co.nz> wrote in message
news:opr94hguy55a2sq9 digitalmars.com...
 Is there a way to avoid runtime type checking? For example say I have a
 variadic function that will only accept certain types eg. int, and uint
 currently I have to go something like..

 void fn(...) {
    if (_arguments[i] != typeid(int) &&
        _arguments[i] != typeid(uint)) {
      //error
    }
    //etc
 }

 which tests at runtime and throws an error.

 Instead it'd be nice if I could go

 void fn(...) {
    _argtypes[] = [int,uint];
    //etc
 }

 or

 void fn(...)
 argtypes(int,uint) {
 }
 body {
    //etc
 }

 and at compile time it could check the types being passed to this fn and
 generate an error.

 Regan.

 -- 
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 02 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Fri, 2 Jul 2004 14:56:00 -0700, Walter <newshound digitalmars.com> 
wrote:
 I think what you're asking for is ably handled by using an 'in' contract 
 to
 test the parameters. An advanced compiler could conceivably test the 'in'
 contracts at compile time.
Good idea.. but the spec does not say it *has* to check them at compile time right? Or, what it does if it can check some but not all of them at compile time. If you had: void foo(int a, ...) in { assert(a == 5); assert(_arguments[i] == typeid(int) || _arguments[i] == typeid(uint)); } body { } then it should check the second assert at compile time and the first at runtime (ignoring/not compiling the second in, as it already checked it) Regan.
 "Regan Heath" <regan netwin.co.nz> wrote in message
 news:opr94hguy55a2sq9 digitalmars.com...
 Is there a way to avoid runtime type checking? For example say I have a
 variadic function that will only accept certain types eg. int, and uint
 currently I have to go something like..

 void fn(...) {
    if (_arguments[i] != typeid(int) &&
        _arguments[i] != typeid(uint)) {
      //error
    }
    //etc
 }

 which tests at runtime and throws an error.

 Instead it'd be nice if I could go

 void fn(...) {
    _argtypes[] = [int,uint];
    //etc
 }

 or

 void fn(...)
 argtypes(int,uint) {
 }
 body {
    //etc
 }

 and at compile time it could check the types being passed to this fn and
 generate an error.

 Regan.

 --
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 03 2004