View mode: basic / threaded / horizontal-split · Log in · Help
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
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)
}
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
On 4/11/11, KennyTM~ <kennytm@gmail.com> wrote:
> 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);

But how do you make `SortOutput` known at the calling site?
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
On Apr 12, 11 02:29, Andrej Mitrovic wrote:
> On 4/11/11, KennyTM~<kennytm@gmail.com>  wrote:
>> 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);
>
> But how do you make `SortOutput` known at the calling site?

Right. Andrei's template can't do it. My approach above is simply 
declare an undocumented enum, and use YesOrNo as a tag to statically 
ensure that enum can take 'no' and 'yes'.

I feel this using type just for documentation is bad.
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
Are we talking about readability in a code editor or just the website?
Because without some form of syntax highlighting the function headers
are almost unreadable from the website:
http://i.imgur.com/B5M6u.png

It's a wall of text.
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
On 04/11/2011 04:59 PM, Andrej Mitrovic wrote:
> It looks like an awkward workaround for that feature called named arguments.

True, but only for the case of yes/no; in this case only, the  bool type 
provides proper *constants* which *meaning* is obvious. Else, you need an enum 
anyway, even with named args.
    f = File(path="f.txt", mode=2);

Denis
-- 
_________________
vita es estrany
spir.wikidot.com
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
On 04/11/2011 07:49 PM, Andrej Mitrovic wrote:
> Also, I would rather name this template "choice".

YesOrNo is far better, by making it clear it's a kind of logical choice / 
closed question. "Choice" is super vague.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
On Apr 12, 11 03:05, Andrej Mitrovic wrote:
> Are we talking about readability in a code editor or just the website?

The concern is that 'OpenRight' needs to be documented separately from 
'until'. With 'YesOrNo', documentation of 'OpenRight' can be omitted.

> Because without some form of syntax highlighting the function headers
> are almost unreadable from the website:
> http://i.imgur.com/B5M6u.png
>
> It's a wall of text.

Well that's a separate issue. Firstly the struct Until shouldn't appear 
(e.g. use 'auto' return). Then the signature appeared would become

    until(alias pred="a==b", Range, Sentinel)(Range range, Sentinel 
sentinel, OpenRight openRight = OpenRight.yes);
    until(alias pred, Range)(Range range, OpenRight openRight = 
OpenRight.yes);

This would break half of the wall.
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
spir:

> True, but only for the case of yes/no; in this case only, the  bool type 
> provides proper *constants* which *meaning* is obvious. Else, you need an enum 
> anyway, even with named args.
>      f = File(path="f.txt", mode=2);

Right, in some cases I prefer an enum and in some cases a named argument.

Bye,
bearophile
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
On 04/11/2011 08:16 PM, KennyTM~ wrote:
> 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)
> }

Very nice, indeed. Also the side-effect that forces the caller to write 
readable code ;-)

Denis
-- 
_________________
vita es estrany
spir.wikidot.com
April 11, 2011
Re: YesOrNo: useful idiom helper or wanking?
On 04/11/2011 08:16 PM, KennyTM~ wrote:
> 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)
> }

If this gets used, would be good style to name the enum using the argument name 
Capitalised, just like in your example (redraw --> Redraw).

Denis
-- 
_________________
vita es estrany
spir.wikidot.com
1 2 3
Top | Discussion index | About this forum | D home