Created
February 12, 2016 01:17
-
-
Save griotspeak/a19297cd74cd447f3a34 to your computer and use it in GitHub Desktop.
n-ary assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol PayloadBearing_1 { | |
typealias Payload_1 : Equatable | |
func payload() -> Payload_1? | |
} | |
protocol PayloadBearing_2 : PayloadBearing_1 { | |
typealias Payload_2 : Equatable | |
func payload() -> (Payload_1, Payload_2)? | |
} | |
protocol PayloadBearing_3 : PayloadBearing_2 { | |
typealias Payload_3 : Equatable | |
func payload() -> (Payload_1, Payload_2, Payload_3)? | |
} | |
enum Foo : PayloadBearing_2 { | |
case One(Int) | |
case Two(Int, Int) | |
func payload() -> Int? { | |
if case let .One(value) = self { | |
return value | |
} else { | |
return nil | |
} | |
} | |
func payload() -> (Int, Int)? { | |
if case let .Two(value) = self { | |
return value | |
} else { | |
return nil | |
} | |
} | |
} | |
func assertSomething<T : PayloadBearing_2>(t: T, v: T.Payload_1?) -> Bool { | |
return t.payload() == v | |
} | |
func assertSomething<T : PayloadBearing_2>(t: T, v: (T.Payload_1, T.Payload_2)?) -> Bool { | |
switch (t.payload(), v) { | |
case let (lhs?, rhs?) : | |
return ((lhs.0 == rhs.0) && | |
(lhs.1 == rhs.1)) | |
case (.None, .None): | |
return true | |
case (.Some, .None), (.None, .Some): | |
return false | |
} | |
} | |
assertSomething(Foo.Two(3, 9), v: (3, 9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment