www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - variadic request on the wild side.

reply Fredrik Olsson <peylow gmail.com> writes:
Having a constructor like:

this (...) { /* some code */ }


Can be useful, but there is one problem. Lets say you expect floats, but 
then constants are by default double, so you have to cast them when 
passing them, or check and cast inside. The variadic function can become 
very complex.

What if I could specify a set of types that should be allowed? Say with 
this syntax as a suggestion:

this (...<int, Set!(int), Range!(int)>) { /* less code */ }

This way the compiler will limit the types that can be passed to the 
function, and the function (in this case a constructor) can be made much 
simpler.


// Fredrik Olsson
Aug 12 2006
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Fredrik Olsson wrote:
 Having a constructor like:
 
 this (...) { /* some code */ }
 
 
 Can be useful, but there is one problem. Lets say you expect floats, but 
 then constants are by default double, so you have to cast them when 
 passing them, or check and cast inside. The variadic function can become 
 very complex.
 
 What if I could specify a set of types that should be allowed? Say with 
 this syntax as a suggestion:
 
 this (...<int, Set!(int), Range!(int)>) { /* less code */ }
 
 This way the compiler will limit the types that can be passed to the 
 function, and the function (in this case a constructor) can be made much 
 simpler.
 
 
 // Fredrik Olsson
It is an interesting idea, although in the case of the example you can already just do: Yes, the floats will get implicitly cast to doubles, but if your constants are doubles anyway... It would be nifty, though, to be able to give a set of acceptable types. I guess it'd be a sugar-shortcut for a Static If and some Is Expressions. -- Chris Nicholson-Sauls
Aug 12 2006