www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Something between a scalar and an enum

An enumeration contains a small number of distinct elements:

enum Colors { red, green, yellow, brown }


While an integral numeric value encodes a scalar:

uint x;
x = 120;
x++;


A sufficiently common pattern in my code is to have something 
intermediate: that has a small group of special values, plus many 
values identified just by a number.


alias Unit = uint;
enum : Uint { EmptyUnit = 0, BrokenUnit = Unit.max - 1, 
MissingUnit = Unit.max }

This allows me to denote some special units, plus count the units 
starting from 1 up to millions or more.

It's unfortunate the D type system doesn't offer me much good to 
express this idiom, but I am not sure how much a type system can 
help here even in principle.

Bye,
bearophile
Nov 24 2014