Named parameters

Johannes Pfau via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 25 08:16:23 PDT 2015


Am Sat, 25 Jul 2015 14:05:32 +0000
schrieb "Martin Nowak" <code at dawg.eu>:

> On Saturday, 25 July 2015 at 13:52:09 UTC, Andrei Alexandrescu 
> wrote:
> >> For one thing, it avoids the ugly hack which is the Flag 
> >> template and
> >> Yes/No structs.
> >>
> >> With Flag:
> >>
> 
> The idiom goes like this.
> 
> /// Flag to control whether or not to keep line endings
> alias KeepTerminator = Flag!"KeepTerminator";
> ///
> string getLine(KeepTerminator KeepTerminator);
> 
> usage:
> 
> getLine(KeepTerminator.yes);
> or
> getLine(Yes.keepTerminator);
> 
> So named Boolean flags are a solved problem and are a good 
> improvement over getLine(true) IMO.

Named boolean flags are only one very common special case though. That
solution doesn't work that well for integers:

auto w = Window(0, 0, 100, 200);
auto w = Window(x = 0, y = 0, width = 100, height = 200);
//auto w = Window(x.0, y.0, width.100, height.200);

You could introduce new types for X,Y, Width, Height:
auto w = Window(X(0), Y(0), Width(100), Height(200));

It doesn't look that bad. But you'll also have to make sure it converts
back to the correct integer types, operator overloading, ... In the end
you always introduce new types for something that is one type with
named parameters. Having extra types is sometimes the correct thing,
sometimes it's not.


More information about the Digitalmars-d mailing list