Thread overview
Variadic function idea...
Jun 24, 2004
Regan Heath
Jul 02, 2004
Walter
Jul 03, 2004
Regan Heath
June 24, 2004
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/
July 02, 2004
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/


July 03, 2004
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/