www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum true, or 1

reply Brian Tiffin <bwtiffin gmail.com> writes:
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.
Jul 21 2021
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 22/07/2021 3:44 PM, 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?
This is the first time I'm hearing about it, so almost certainly no.
Jul 21 2021
prev sibling next sibling parent Mathias LANG <geod24 gmail.com> writes:
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.
Jul 21 2021
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jul 21 2021
parent reply Brian Tiffin <bwtiffin gmail.com> writes:
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).
Jul 22 2021
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/22/21 1:44 PM, Brian Tiffin wrote:

 So would there be any cringes
Not from me. :)
 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.
I don't feel qualified to comment on such a topic but it looks fine. Perhaps INDEMNIFIED is more grammatically correct but weirder too. :) Ali
Jul 23 2021