enum true, or 1
Mathias LANG
geod24 at gmail.com
Thu Jul 22 03:56:33 UTC 2021
On Thursday, 22 July 2021 at 03:44:13 UTC, Brian Tiffin wrote:
> What is the preferred syntax for simple on/off states? Did I
> read that D was moving to strictly 1 and 0 literals instead of
> true/false on/off yes/no?
>
> If so, is there an idiom for yes/no/maybe -1,0,1
> less/equal/greater?
>
> Excuse the noise. For some backfill; getting used to DDoc.
> Frontmatter can be attached to the module declaration. Great
> for setting up a page. Wanted to do something similar for
> backmatter, in particular the without warranty disclaimer
> (instead of it taking up space on the first screen of a
> listing). That means attaching backmatter doc comments to a
> declaration, something akin to `enum INDEMNITY = true;`. Or is
> it `= 1`?
>
> Opinions welcome, as this will end up being nothing more than a
> personal preference, but it would be nice to avoid dragging
> fingernails over anyone's mental chalkboard.
>
> Have good, make well.
AFAIK, the usual wisdom is to use `true` or `false` over `1` and
`0`.
However, it still might not be enough in a public API, so there
is `std.typecons.Flag`:
https://dlang.org/library/std/typecons/flag.html
Last but not least, `true` is defined as `1` per the spec, so the
following is valid:
```
char[] myString = getString();
bool hasNullCharAtEnd = parseString(myString);
char[] copy = new char[](myString.length - hasNullCharAtEnd);
```
This is quite useful when needed to offset something by one: Just
add or subtract your `bool` value.
More information about the Digitalmars-d-learn
mailing list