enum true, or 1
Brian Tiffin
bwtiffin at gmail.com
Thu Jul 22 20:44:59 UTC 2021
On Thursday, 22 July 2021 at 03:58:38 UTC, Ali Çehreli wrote:
> On 7/21/21 8:44 PM, Brian Tiffin wrote:
>
> > What is the preferred syntax for simple on/off states?
>
> I use std.typecons.Flag. It feels very repetitive but that's
> what we have:
>
> import std.typecons;
>
> void foo(Flag!"doFilter" doFilter) {
> if (doFilter) {
> // ...
> }
> }
>
> bool someCondition;
>
> void main() {
> // Literal values of Flag!"doFilter"
> foo(Yes.doFilter);
> foo(No.doFilter);
>
> // Assuming we start with an existing 'bool'...
> bool doFilter = someCondition;
> // ... I like this helper (see below):
> foo(flagFromBool!doFilter);
> }
>
> template flagFromBool(alias variable) {
> enum flagName = variable.stringof;
>
> auto flagFromBool() {
> import std.format : format;
>
> enum expr = format!q{
> return variable ? Yes.%s : No.%s;
> }(flagName, flagName);
>
> mixin (expr);
> }
> }
>
> > Did I read that
> > D was moving to strictly 1 and 0 literals instead of
> true/false on/off
> > yes/no?
>
> Walter sees 'bool' as a 1-bit integer type. I can't. :) I
> cringe everytime I see assert(0). To me, it is assert(false); :/
>
> > If so, is there an idiom for yes/no/maybe -1,0,1
> less/equal/greater?
>
> opCmp's return value has three states:
>
> < 0
> == 0
> > 0
>
> Ali
So would there be any cringes seeing a skeleton D source file
that always ended with
~~~d
/++
Disclaimer
This software is distributed in the hope that it will be
useful, but
WITHOUT ANY WARRANTY; direct or indirect, without even the
implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
+/
enum INDEMNITY = true;
~~~ ??
The goal is attaching backmatter doc comments to a declaration
that follows without being too intrusive.
Thanks for the wisdoms, Ali (and Mathias).
More information about the Digitalmars-d-learn
mailing list