Passing Arguments on in Variadic Functions

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 17 14:27:29 PDT 2015


On Thursday, 17 September 2015 at 18:40:56 UTC, Adam D. Ruppe 
wrote:
>
> I would actually just make it required or do separate functions 
> with it. Optional and variadics don't mix well together.
>
> (You could also loop through and look for a bool argument 
> yourself but that is a bit messier.)
>

It's a result of dealing with the sum function. The seed 
parameter will override the type. So for instance one option is 
to do something like
auto test(T)(T x,  bool sample=true, T seed=T.init)
ignoring the complexity of T.init being a nan for some types, it 
would also overrule some of the behavior of sum. For instance, if 
you do sum(x) where x is a dynamic array of floats, then the 
result is a double instead of a float.

I think I could figure out how to look through the arguments for 
a bool, but wouldn't that make me give up the default value for 
the bool?

Maybe it would require, doing one version of the function defined 
like
auto test(T)(T x,  bool sample=true)
and then another like
auto test(T, U ...)(T x,  U y)
I could then try to use static ifs to ensure some of the other 
requirements (like a restriction on length so that it's not 
called if there is only one argument in y). I probably don't even 
need to loop, I can just static if that the first one in y is a 
bool and the second is numeric.


More information about the Digitalmars-d-learn mailing list