Proposal: Variable invariants


(Amir Michail) #1

Examples:

var x:(Int where 1…10 ~= x) = 5
var y:(Double where trunc(y*2) == y*2) = 3.5

x = 11 // run-time error
y = 3.6 // run-time error

typealias T = Double(y) where trunc(y*2) == y*2
var y2:T = 4.5

// type for 10x10 grid of integers
typealias Grid = [[Int]](g) where g.indices == 0..<10 && g[i].indices == 0..<10 for i in g.indices