digitalmars.D - variadic request on the wild side.
- Fredrik Olsson (13/13) Aug 12 2006 Having a constructor like:
- Chris Nicholson-Sauls (7/28) Aug 12 2006 It is an interesting idea, although in the case of the example you can a...
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
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 OlssonIt 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