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

Reply via email to