YesOrNo: useful idiom helper or wanking?

Stewart Gordon smjg_1998 at yahoo.com
Mon Apr 11 13:55:38 PDT 2011


On 11/04/2011 18:13, Andrej Mitrovic wrote:
> On 4/11/11, Andrei Alexandrescu<SeeWebsiteForEmail at erdani.org>  wrote:
>> template YesOrNo(string name)
>> {
>>       mixin("enum "~name~" : bool { no, yes }");
>> }
>>
>> void someFunction(YesOrNo!"SomeOption") { }
>
> How exactly would this work? I can't compile it.

I make out the idea to be that, at the module or class level, you write

     mixin YesOrNo!("SomeOption");

and then declare functions as, e.g.

     void doSomething(SomeOption someOption);

and so calls to this function become self-documenting with

     doSomething(SomeOption.yes);

The disadvantage is that a user of the library needs to look up SomeOption in order to 
find out what the possible values are, whereas everybody knows what the possible values of 
a bool are.

It's really a workaround for two things:
- named arguments
- strong typedefs

Stewart.


More information about the Digitalmars-d mailing list