Range checked assignment

Cecil Ward cecil at cecilward.com
Tue Sep 8 14:18:14 UTC 2020

What I would like to do is (in pseudo-code) :

    declare_var my_var : int range 0..7; // i.e. 0 <= val <= 7;

     my_var = 6; // ok
     my_var = 8; // bang ! static assert fail or assert fail at 

     my_var = 6;
     my_var += 2; // bang ! value 8 is > 7

So every assignment is range-checked at either compile-time if at 
all possible or else at runtime. This includes things like += and 
initialisers of course, not just straight assignment.

I assumed I would have to create a struct type definition and 
handle various operators. How many will I have to handle? I would 
of course make it a template so I can reuse this otherwise 
horribly repetitive code.

More information about the Digitalmars-d-learn mailing list