From the D Blog: Crafting Self-Evident Code in D

Salih Dincer salihdb at hotmail.com
Fri Oct 27 03:12:10 UTC 2023


On Saturday, 7 October 2023 at 12:37:37 UTC, sighoya wrote:
> I disagree however in all binary types should be just boolean.
> I prefer machineState=State.On or State.Off than 
> isMachineOn=true or false.

This was finished possible:


```d
import std;

enum State : bool
{
   Off, On
}

void main()
{
   State machineState;
   "The machine ".write;

   if(machineState == State.On) {
     "may be ".write;
   }
   machineState = State.On;
   
   if(machineState == State.On) {
     "definitely ".write; 
   }
   "runnning!".writeln;

// BONUS: TriState
    int engineHP = -500;
    "The engine ".write;
   
    final switch(engineHP.sgn)
    {
      case TriState.Off: "was off!".writeln; break;
      case TriState.Start: "was start!".writeln; break;
      case TriState.On: "running!".writeln; break;
    }
}

enum TriState
{
     Off = -1, Start, On
}
```
SDB79


More information about the Digitalmars-d-announce mailing list