Subrange type

Paul Backus snarwin at gmail.com
Mon Jan 27 21:01:58 UTC 2020


On Monday, 27 January 2020 at 20:06:14 UTC, Herbert wrote:
> How can I create a subrange type, for example ushort DiceValue 
> {1 .. 6}?

Probably the closest you can get is a struct with an invariant:

import std.traits: isOrderingComparable;

struct Subrange(T, T min, T max)
     if (isOrderingComparable!T)
{
     private T value_ = min;
     invariant(min <= value && value <= max);

     this(T t) { value_ = t; }

     T value() { return value_; }
     alias value this;

     ref Subrage opAssign(T t) { value_ = t; return this; }
}


More information about the Digitalmars-d-learn mailing list