module Constraints {
// define a constraint for a type to constrain it's values at the time
// of construction.
//
// a constraint should throw an exception if the value does not meet
// the constraint. constraints should have a single parameter whose
// type is the type that is being constrained.
//
// Constraints may not be used with mut types.
// define Size to be a uint that must be greater than 0
typedef Size = uint;
except SizeMustBePositive{}
constraint(this : Size) {
if (this.size <= 0) {
throw SizeMustBePositive;
}
}
fn test1() {
let s1 : Size = 1; // ok
let s2 : Size = 0; // exception SizeMustBePositive
}
struct Dimensions {
width : Size;
height : Size;
}
fn test2() {
let d1 = Dimensions(1, 1); // ok
let d2 = Dimensions(1, 0); // exception SizeMustBePositive
}
}