digitalmars.D - YesOrNo: useful idiom helper or wanking?
- Andrei Alexandrescu (26/26) Apr 11 2011 A fair amount of code in std uses this idiom:
- Andrej Mitrovic (1/1) Apr 11 2011 It looks like an awkward workaround for that feature called named argume...
- Adam D. Ruppe (5/7) Apr 11 2011 Named arguments aren't a part of standard D now, nor does it look
- Andrej Mitrovic (2/2) Apr 11 2011 They aren't a part of D as long as we try to avoid them with
- KennyTM~ (5/12) Apr 11 2011 Well the same argument was used to reject assertPred in favor of an
- FeepingCreature (3/42) Apr 11 2011 Named parameters fix this without the need for workarounds.
- Dmitry Olshansky (18/43) Apr 11 2011 Well, it's workaround but an interesting one.
- Andrej Mitrovic (2/7) Apr 11 2011 How exactly would this work? I can't compile it.
- Dmitry Olshansky (5/13) Apr 11 2011 Yeah it's problematic, making it eponymous make function definition
- Stewart Gordon (14/22) Apr 11 2011 I make out the idea to be that, at the module or class level, you write
- Andrej Mitrovic (8/8) Apr 11 2011 Also, I would rather name this template "choice". Maybe if people got
- KennyTM~ (29/37) Apr 11 2011 The idea, IIUC, is to avoid documenting that extra enum type. So, for
- Andrej Mitrovic (2/9) Apr 11 2011 But how do you make `SortOutput` known at the calling site?
- KennyTM~ (5/17) Apr 11 2011 Right. Andrei's template can't do it. My approach above is simply
- Andrej Mitrovic (5/5) Apr 11 2011 Are we talking about readability in a code editor or just the website?
- KennyTM~ (10/15) Apr 11 2011 The concern is that 'OpenRight' needs to be documented separately from
- Nick Sabalausky (3/16) Apr 11 2011 ...And suddenly people won't know what the hell to expect it to return.
- Andrei Alexandrescu (5/24) Apr 11 2011 I think it's not all that bad to state until returns a range with such
- KennyTM~ (3/22) Apr 11 2011 That's why you assign the result using 'auto'.
- Andrei Alexandrescu (4/16) Apr 11 2011 I was hasty. I can't see a way for a mixin to introduce at the same time...
- spir (8/23) Apr 11 2011 Very nice, indeed. Also the side-effect that forces the caller to write
- spir (8/22) Apr 11 2011 If this gets used, would be good style to name the enum using the argume...
- spir (10/11) Apr 11 2011 True, but only for the case of yes/no; in this case only, the bool type...
- bearophile (4/8) Apr 11 2011 Right, in some cases I prefer an enum and in some cases a named argument...
- spir (8/9) Apr 11 2011 YesOrNo is far better, by making it clear it's a kind of logical choice ...
- Nick Sabalausky (19/44) Apr 11 2011 Named args are much better for this. No boilerplate needed. Of course,
- spir (8/63) Apr 11 2011 I now think the same way.
- Andrei Alexandrescu (3/8) Apr 11 2011 Never thought of it. Facepalm etc. Thanks!
- Bernard Helyer (2/2) Apr 13 2011 Named arguments. Seriously. Named arguments.
A fair amount of code in std uses this idiom: enum SomeOption { no, yes } void someFunction(...parms..., SomeOption, ...more_parms...) { ... } SomeOption is really a Boolean but replaces the unhelpful call syntax someFunction(...args..., false, ...more_args...) with the self-documenting ...args..., SomeOption,no, ...more_args...). The liabilities of using Booleans for describing flags have been documented in several places, including McConnell's Code Complete. I think the idiom above is a good replacement. One issue I have with it is that it requires separate definition and documentation. Usually the documentation awkwardly states "This type is really an option for someFunction, which you haven't seen yet. Skip this for now, then get back, and you'll understand." Moving the enum to after the function is possible but equally confusing. So I was thinking of planting a simple template in std.typecons: template YesOrNo(string name) { mixin("enum "~name~" : bool { no, yes }"); } That way, the definition of the function needs no separately-defined is self-documenting: void someFunction(...parms..., YesOrNo!"SomeOption", ...) { ... } The question is to what extent this would mark progress and fostering structured use of this idiom, versus just confusing beginners and adding deadweight to Phobos. Andrei
Apr 11 2011
It looks like an awkward workaround for that feature called named arguments.
Apr 11 2011
Andrej Mitrovic wrote:It looks like an awkward workaround for that feature called named arguments.Named arguments aren't a part of standard D now, nor does it look likely they will be for many years. We've gotta look at the here and now to make decisions; focus on what we have rather than what we don't have.
Apr 11 2011
They aren't a part of D as long as we try to avoid them with workarounds that make functions look like crap.
Apr 11 2011
On Apr 11, 11 23:27, Adam D. Ruppe wrote:Andrej Mitrovic wrote:Well the same argument was used to reject assertPred in favor of an improved assert which we still don't have (issue 5547). (Though changing assert's output doesn't change the D spec, while adding named arguments do, and the D2 spec is frozen.)It looks like an awkward workaround for that feature called named arguments.Named arguments aren't a part of standard D now, nor does it look likely they will be for many years. We've gotta look at the here and now to make decisions; focus on what we have rather than what we don't have.
Apr 11 2011
On 11.04.2011 16:53, Andrei Alexandrescu wrote:A fair amount of code in std uses this idiom: enum SomeOption { no, yes } void someFunction(...parms..., SomeOption, ...more_parms...) { ... } SomeOption is really a Boolean but replaces the unhelpful call syntax someFunction(...args..., false, ...more_args...) with the self-documenting ...args..., SomeOption,no, ...more_args...). The liabilities of using Booleans for describing flags have been documented in several places, including McConnell's Code Complete. I think the idiom above is a good replacement. One issue I have with it is that it requires separate definition and documentation. Usually the documentation awkwardly states "This type is really an option for someFunction, which you haven't seen yet. Skip this for now, then get back, and you'll understand." Moving the enum to after the function is possible but equally confusing. So I was thinking of planting a simple template in std.typecons: template YesOrNo(string name) { mixin("enum "~name~" : bool { no, yes }"); } That way, the definition of the function needs no separately-defined is self-documenting: void someFunction(...parms..., YesOrNo!"SomeOption", ...) { ... } The question is to what extent this would mark progress and fostering structured use of this idiom, versus just confusing beginners and adding deadweight to Phobos. AndreiNamed parameters fix this without the need for workarounds. someFunction(...parms..., someOption=>true, ...more parms....);
Apr 11 2011
On 11.04.2011 18:53, Andrei Alexandrescu wrote:A fair amount of code in std uses this idiom: enum SomeOption { no, yes } void someFunction(...parms..., SomeOption, ...more_parms...) { ... } SomeOption is really a Boolean but replaces the unhelpful call syntax someFunction(...args..., false, ...more_args...) with the self-documenting ...args..., SomeOption,no, ...more_args...). The liabilities of using Booleans for describing flags have been documented in several places, including McConnell's Code Complete. I think the idiom above is a good replacement. One issue I have with it is that it requires separate definition and documentation. Usually the documentation awkwardly states "This type is really an option for someFunction, which you haven't seen yet. Skip this for now, then get back, and you'll understand." Moving the enum to after the function is possible but equally confusing. So I was thinking of planting a simple template in std.typecons: template YesOrNo(string name) { mixin("enum "~name~" : bool { no, yes }"); } That way, the definition of the function needs no separately-defined is self-documenting: void someFunction(...parms..., YesOrNo!"SomeOption", ...) { ... } The question is to what extent this would mark progress and fostering structured use of this idiom, versus just confusing beginners and adding deadweight to Phobos.Well, it's workaround but an interesting one. I presume the user side remains intact and it's just enum declaration that got inculcated into the someFunction declaration, right? (I suspect some folks on the NG might be puzzled by it so I'll try to clarify some points) Like: someFunction(...,SomeOption.yes,...); Then yes it marks certain progress, and I like it. BTW double definition is also nicely solved (i.e. it would be the same template instance). YesOrNo!"Direction" would be the same for all function dealing with "Direction" parameter. Then to save lives of novices reading API docs, we should have some nice short memo about this idiom on the language page or even in the Phobos module listing "common idioms" if we have lots of them, plus links to it everywhere applicable. -- Dmitry Olshansky
Apr 11 2011
On 4/11/11, Andrei Alexandrescu <SeeWebsiteForEmail 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.
Apr 11 2011
On 11.04.2011 21:13, Andrej Mitrovic wrote:On 4/11/11, Andrei Alexandrescu<SeeWebsiteForEmail erdani.org> wrote:Yeah it's problematic, making it eponymous make function definition work, but at the calling site the party ends... -- Dmitry Olshanskytemplate YesOrNo(string name) { mixin("enum "~name~" : bool { no, yes }"); } void someFunction(YesOrNo!"SomeOption") { }How exactly would this work? I can't compile it.
Apr 11 2011
On 11/04/2011 18:13, Andrej Mitrovic wrote:On 4/11/11, Andrei Alexandrescu<SeeWebsiteForEmail erdani.org> wrote: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.template YesOrNo(string name) { mixin("enum "~name~" : bool { no, yes }"); } void someFunction(YesOrNo!"SomeOption") { }How exactly would this work? I can't compile it.
Apr 11 2011
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..
Apr 11 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) }
Apr 11 2011
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?
Apr 11 2011
On Apr 12, 11 02:29, Andrej Mitrovic wrote:On 4/11/11, KennyTM~<kennytm gmail.com> wrote: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.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?
Apr 11 2011
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.
Apr 11 2011
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.
Apr 11 2011
"KennyTM~" <kennytm gmail.com> wrote in message news:invke1$2gv6$1 digitalmars.com...On Apr 12, 11 03:05, Andrej Mitrovic wrote:...And suddenly people won't know what the hell to expect it to return.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.
Apr 11 2011
On 4/11/11 2:41 PM, Nick Sabalausky wrote:"KennyTM~"<kennytm gmail.com> wrote in message news:invke1$2gv6$1 digitalmars.com...I think it's not all that bad to state until returns a range with such and such characteristics. After all this is the norm in dynamically typed languages. AndreiOn Apr 12, 11 03:05, Andrej Mitrovic wrote:...And suddenly people won't know what the hell to expect it to return.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.
Apr 11 2011
On Apr 12, 11 03:41, Nick Sabalausky wrote:"KennyTM~"<kennytm gmail.com> wrote in message news:invke1$2gv6$1 digitalmars.com...That's why you assign the result using 'auto'. (This was discussed before, but I can't find the post.)On Apr 12, 11 03:05, Andrej Mitrovic wrote:...And suddenly people won't know what the hell to expect it to return.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.
Apr 11 2011
On 4/11/11 1:29 PM, Andrej Mitrovic wrote:On 4/11/11, KennyTM~<kennytm gmail.com> wrote:I was hasty. I can't see a way for a mixin to introduce at the same time SortOutput and itself in scope. AndreiThe 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?
Apr 11 2011
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
Apr 11 2011
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
Apr 11 2011
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
Apr 11 2011
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
Apr 11 2011
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
Apr 11 2011
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:inv4rv$1dfl$1 digitalmars.com...A fair amount of code in std uses this idiom: enum SomeOption { no, yes } void someFunction(...parms..., SomeOption, ...more_parms...) { ... } SomeOption is really a Boolean but replaces the unhelpful call syntax someFunction(...args..., false, ...more_args...) with the self-documenting ...args..., SomeOption,no, ...more_args...). The liabilities of using Booleans for describing flags have been documented in several places, including McConnell's Code Complete. I think the idiom above is a good replacement.Named args are much better for this. No boilerplate needed. Of course, without named args, yea, this idiom needs to be used.One issue I have with it is that it requires separate definition and documentation. Usually the documentation awkwardly states "This type is really an option for someFunction, which you haven't seen yet. Skip this for now, then get back, and you'll understand." Moving the enum to after the function is possible but equally confusing./// Documentation here enum SomeOption { no, yes } ///ditto void someFunction(...parms..., SomeOption, ...more_parms...) { ... } That groups the two together, right? So solved.So I was thinking of planting a simple template in std.typecons: template YesOrNo(string name) { mixin("enum "~name~" : bool { no, yes }"); } That way, the definition of the function needs no separately-defined is self-documenting: void someFunction(...parms..., YesOrNo!"SomeOption", ...) { ... } The question is to what extent this would mark progress and fostering structured use of this idiom, versus just confusing beginners and adding deadweight to Phobos.Not sure what I think, really. Although, I do think the fact there seems to be a need for someting that complex just for a mere two-option setting is more indication that named args are just a better approach. But since we're stuck without that, I really don't know how I feel about this template. Umm, if you need to add a third setting, then you're right back to using a straight enum and making sure it's documentated sensibly. You could generalize YesOrNo further, but I don't think's possible without making it really ugly to use. So maybe it's better to just make sure there's a good way to handle the documentation. If that turns out to require some new DDoc feature, then so be it.
Apr 11 2011
On 04/11/2011 09:35 PM, Nick Sabalausky wrote:"Andrei Alexandrescu"<SeeWebsiteForEmail erdani.org> wrote in message news:inv4rv$1dfl$1 digitalmars.com...Yop! The "ditto" feature is great for such cases.A fair amount of code in std uses this idiom: enum SomeOption { no, yes } void someFunction(...parms..., SomeOption, ...more_parms...) { ... } SomeOption is really a Boolean but replaces the unhelpful call syntax someFunction(...args..., false, ...more_args...) with the self-documenting ...args..., SomeOption,no, ...more_args...). The liabilities of using Booleans for describing flags have been documented in several places, including McConnell's Code Complete. I think the idiom above is a good replacement.Named args are much better for this. No boilerplate needed. Of course, without named args, yea, this idiom needs to be used.One issue I have with it is that it requires separate definition and documentation. Usually the documentation awkwardly states "This type is really an option for someFunction, which you haven't seen yet. Skip this for now, then get back, and you'll understand." Moving the enum to after the function is possible but equally confusing./// Documentation here enum SomeOption { no, yes } ///ditto void someFunction(...parms..., SomeOption, ...more_parms...) { ... } That groups the two together, right? So solved.I now think the same way. Denis -- _________________ vita es estrany spir.wikidot.comSo I was thinking of planting a simple template in std.typecons: template YesOrNo(string name) { mixin("enum "~name~" : bool { no, yes }"); } That way, the definition of the function needs no separately-defined is self-documenting: void someFunction(...parms..., YesOrNo!"SomeOption", ...) { ... } The question is to what extent this would mark progress and fostering structured use of this idiom, versus just confusing beginners and adding deadweight to Phobos.Not sure what I think, really. Although, I do think the fact there seems to be a need for someting that complex just for a mere two-option setting is more indication that named args are just a better approach. But since we're stuck without that, I really don't know how I feel about this template. Umm, if you need to add a third setting, then you're right back to using a straight enum and making sure it's documentated sensibly. You could generalize YesOrNo further, but I don't think's possible without making it really ugly to use. So maybe it's better to just make sure there's a good way to handle the documentation. If that turns out to require some new DDoc feature, then so be it.
Apr 11 2011
On 4/11/11 2:35 PM, Nick Sabalausky wrote:/// Documentation here enum SomeOption { no, yes } ///ditto void someFunction(...parms..., SomeOption, ...more_parms...) { ... } That groups the two together, right? So solved.Never thought of it. Facepalm etc. Thanks! Andrei
Apr 11 2011
Named arguments. Seriously. Named arguments. *stares into Andrei's soul*
Apr 13 2011