Skip to content

Instantly share code, notes, and snippets.

@ttepasse
Created December 12, 2014 16:29
Show Gist options
  • Save ttepasse/141da0012f9daabf9e9c to your computer and use it in GitHub Desktop.
Save ttepasse/141da0012f9daabf9e9c to your computer and use it in GitHub Desktop.
// 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