Flag proposal

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Jun 10 10:24:36 PDT 2011


On 6/10/11 11:45 AM, Andrej Mitrovic wrote:
> I like how your proposal is already a pull request.
>
> I'm not too fond of this workaround:
>
> bool state;
> getopt(
>      args,
>      "state",&state,
> );
> auto line = getLine(cast(Flag!"KeepTerminator")state);
>
> Will getopt know how to use Flags? What about std.conv.to?

Yes, getopt should support any enum using bool as its base. std.conv 
already supports all enums.

> std.format/write will have to know how to print out a Flag too, or we
> have to cast:
> writeln(cast(bool)keepTerminator);

Without the cast, writeln prints "yes" or "no", which may be desirable. 
If you want to print the bool, you can say

writeln(keepTerminator == Flag!"KeepTerminator".yes);

or

writeln(!!keepTerminator);

etc. I find this array of options quite sensible.

> If the plan is to replace all boolean arguments in Phobos with Flags,
> won't this break a lot of code?

Migration can be done piecemeal with deprecation and all.

> We'll have to switch to using this
> template, and then if we finally get named arguments sometime down the
> road we will have to convert everything back to bools again.

If we convert back to bool the problem remains that people still can 
just pass "true" or whatever without specifying the name of the 
parameter. True, they could and should specify the name of the 
parameter, but by that argument they could and should insert a comment 
right now - and nobody does.


Andrei


More information about the Digitalmars-d mailing list