Creating a "fixed-range int" with opDispatch and/or alias this?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 2 07:59:38 PDT 2016


On 06/01/2016 12:59 PM, Mark Isaacson wrote:
> I'm trying to create a type that for all intents and purposes behaves
> exactly like an int except that it limits its values to be within a
> certain range [a,b].

'alias this' with property functions work at least for your example:


struct FixedRangeInt {
     int min;
     int max;
     int i_;

     int value() const {
         return i_;
     }

     void value(int i) {
         assert(i >= min);
         assert(i < max);
         i_ = i;
     }

     alias i_ this;
}

void main() {
     auto f = FixedRangeInt(0, 100);
     f += 2;
     assert(f == 2);
}

Ali



More information about the Digitalmars-d-learn mailing list