[swift-evolution] Proposal: Variable invariants

Amir Michail a.michail at me.com
Thu Dec 31 17:04:59 CST 2015


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


More information about the swift-evolution mailing list