Created
December 12, 2014 16:29
-
-
Save ttepasse/141da0012f9daabf9e9c to your computer and use it in GitHub Desktop.
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
// I find no better version than manually writing unwrap 1...n | |
func unwrap <T> (first: T?, closure: (T)->()) -> () { | |
if let p1 = first { | |
closure(p1) | |
} | |
} | |
func unwrap <T1, T2> (first: T1?, second: T2?, closure: (T1, T2)->()) -> () { | |
var unwrappedFirst: T1, unwrappedSecond: T2 | |
if let x = first { unwrappedFirst = x } else { return } | |
if let x = second { unwrappedSecond = x } else { return } | |
closure(unwrappedFirst, unwrappedSecond) | |
} | |
func unwrap <T1, T2, T3> (first: T1?, second: T2?, third: T3?, closure: (T1, T2, T3)->()) -> () { | |
var unwrappedFirst: T1, unwrappedSecond: T2, unwrappedThird: T3 | |
if let x = first { unwrappedFirst = x } else { return } | |
if let x = second { unwrappedSecond = x } else { return } | |
if let x = third { unwrappedThird = x } else { return } | |
closure(unwrappedFirst, unwrappedSecond, unwrappedThird) | |
} | |
func unwrap <T1, T2, T3, T4> (first: T1?, second: T2?, third: T3?, fourth: T4?, closure: (T1, T2, T3, T4)->()) -> () { | |
var unwrappedFirst: T1, unwrappedSecond: T2, unwrappedThird: T3, unwrappedFourth: T4 | |
if let x = first { unwrappedFirst = x } else { return } | |
if let x = second { unwrappedSecond = x } else { return } | |
if let x = third { unwrappedThird = x } else { return } | |
if let x = fourth { unwrappedFourth = x } else { return } | |
closure(unwrappedFirst, unwrappedSecond, unwrappedThird, unwrappedFourth) | |
} | |
class A {} | |
class B {} | |
class C {} | |
var a: A? = A() | |
var b: B? = B() | |
var c: C? = C() | |
unwrap(a, b, c) { | |
a, b, c in | |
let x = (a, b, c) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment