YesOrNo: useful idiom helper or wanking?

KennyTM~ kennytm at gmail.com
Mon Apr 11 11:16:02 PDT 2011


On Apr 12, 11 01:49, Andrej Mitrovic wrote:
> Also, I would rather name this template "choice". Maybe if people got
> used to this word they would understand it when they see it in the
> documentation before a function definition. E.g.:
>
> http://codepad.org/9mrL6MOG or if the site is down:
> https://gist.github.com/913926
>
> Otherwise I have no idea how you can put a new type definition inside
> of a function parameter and make it public? I'm kind of confused
> here..

The idea, IIUC, is to avoid documenting that extra enum type. So, for 
example,

     TRange topNCopy(alias less = "a < b", SRange, TRange)
                    (SRange source, TRange target,
                     YesOrNo!"SortOutput" sorted = false);

and then we can call it as

     topNCopy([1,3,5,2,4], result, SortOutput.yes);

and you don't need to generate the documentation of SortOutput, because 
we already know from the YesOrNo template that SortOutput can only take 
'yes' or 'no'. Your approach is no different from defining Color and 
Redraw directly.

--------

If the goal of YesOrNo is simply for documentation, why not define it 
like this?

import std.stdio;

template YesOrNo(T) if(is(T == enum) && !T.no && T.yes) {
     alias T YesOrNo;
}

enum Redraw : bool { no, yes }

void drawCircle(YesOrNo!Redraw redraw) {
     writeln(cast(bool) redraw);
}

void main() {
     drawCircle(Redraw.yes);
     drawCircle(Redraw.no);
     // drawCircle(false);  (cannot implicitly convert expression 
(false) of type bool to Redraw)
}



More information about the Digitalmars-d mailing list