[swift-evolution] Proposal: named invariants for variable declarations

Árpád Goretity arpad.goretity at gmail.com
Tue Jan 5 00:20:21 CST 2016


This suspiciously starts to resemble a mixture of dependent types and explicit contracts (something like the Midori variant of C#).

By the way, while we are at it: if the compiler supports this kind of feature, we could (and should) statically check most of the constraints. Accordingly, in your example, f(-3) would be a compiler error (the two pure invariants applied to the constant argument would both trivially constant-fold to false).

Sent from my iPhone

> On 04 Jan 2016, at 21:33, Howard Lovatt <howard.lovatt at gmail.com> wrote:
> 
> Alternatively the Properties Behaviour syntax proposal could be applied to any declaration, assuming that it is accepted. 
> 
> 
>> On 5 Jan 2016, at 4:18 AM, Amir Michail via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Examples:
>> 
>> invariant vectorIndex(v:Int) { return 0..<3 ~= v }
>> 
>> var i:Int,vectorIndex: = 2
>> i = 3 // run-time error
>> 
>> invariant positive(v:Int) { return v > 0 }
>> invariant odd(v:Int) { return v % 2 == 1 }
>> 
>> var x:Int, positive, odd = 5
>> 
>> x = 2 // run-time error
>> 
>> func f(z:Int, positive, odd) {
>>>> }
>> 
>> f(-3) // run-time error
>> 
>> 
>> 
>> 
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 


More information about the swift-evolution mailing list