Function with default parameters

Mariusz Gliwiński alienballance at gmail.com
Fri Sep 17 16:07:46 PDT 2010


On 2010-09-18 00:40, Steven Schveighoffer wrote:
> On Fri, 17 Sep 2010 18:37:09 -0400, Mariusz Gliwiński
> <alienballance at gmail.com> wrote:
>> Basically picking just right parameter while other have default. But
>> now I can't find syntax anywhere. This code didn't worked, could you
>> refresh my memory?
>
> D parameters don't work this way. You cannot specify a parameter by name.
>
> There are tricks you can use, such as structs with initializers, but the
> result is probably not what you want.

No? That was different language then :(

Why it isn't allowed? Implementing that would be just about checking if 
all required parameters are filled and make something with parameter 
name / local function variable conflict. It would be nice a little nice 
thing...

Or maybe lack of this feature pulling programmer out from throwing into 
function 10 rarely used parameters? Would be a bad thing? What are 
limitations for parameter number? There are any?

Lastly, what's the most preferred way of adding many rarely used 
parameters into function? Been thinking about adding struct for them but 
it's probably not efficient. Been thinking about variadic solution, but 
that's just ugly since I hide possible solutions from programmer. I can 
make solution stateful in the meaning of adding parameters one by one 
because it's a method... I don't like this solution too. Named 
parameters would be great but... absent. As You probably understand 
adding foo(,,,,,,,,,bar,,,) isn't proper solution too :) Any hints?

Thanks,
Mariusz Gliwiński


More information about the Digitalmars-d-learn mailing list