Good morning, list, I know that D has support for ranges in for-each statements and in array bounds checking, but I'm curious if it also has a facility for compile-time range checking or assertions on individual variables.
For example, using the Java Tutorial's venerable Bicycle class, say I had a property <code>int gear;</code> which indicated the bicycle's current gear. Let's say all bicycles of this class have 6 gears. Typically, I would write a changeGear method as: <code> void changeGear(int newGear) { if ( 1 <= newGear && newGear <= 6) gear = newGear; else throw SomeKindOfException(); } </code> which would include the if statement to make sure that newGear would be passed a number in the correct range and a throw statement to deal with cheaters. Does D allow - or would there be logic in creating - an ability to define the acceptable ranges when the variable is initialised? So, say I wanted gear only to accept values between 1 and 6 inclusive. Could I declare something to the effect of: <code>int gear {1...6} = 1;</code> Which could read, "create a new int, called gear, which only accepts values between 1 and 6, inclusive, and initialise it to 1." Then, if I, or some coder who forgot how many gears my Bicycle has, tried to call: </code>bicycle.changeGear( 12 );</code> the compiler would catch that <code>gear = newGear;</code> is assigning a value out of range and throw a compile-time error. Can D do this? Should it do this? With thanks, Borden