Last active
August 29, 2015 14:04
-
-
Save rnapier/0d24ca74e4b4c5bfb3b4 to your computer and use it in GitHub Desktop.
withExtendedLifetimes (arbitrary number of parameters)
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
// You call this as: | |
// withExtendedLifetimes([x,y,z]) { ... } | |
// I haven't been able to replace the array with variadic. The function | |
// will be absorbed into the list (even if it's curried). | |
func withExtendedLifetimes(args: [Any], f: () -> ()) { | |
var rest = args | |
let last:Any = rest.removeLast() | |
if rest.count > 0 { | |
Swift.withExtendedLifetime(last) { () -> () in withExtendedLifetimes(rest, f) } | |
} | |
else { | |
Swift.withExtendedLifetime(last, f) | |
} | |
} | |
//////// | |
// But don't discount the other approach which doesn't require the extra array | |
// (in the spirit of withUnsafePointers): | |
// To get rid of the `() -> () in` casting | |
func withExtendedLifetime<T>(x: T, f: () -> ()) { | |
return Swift.withExtendedLifetime(x, f) | |
} | |
func withExtendedLifetimes<A0, A1>(arg0: A0, arg1: A1, f: () -> ()) { | |
return withExtendedLifetime(arg0) { withExtendedLifetime(arg1, f) } | |
} | |
func withExtendedLifetimes<A0, A1, A2>(arg0: A0, arg1: A1, arg2: A2, f: () -> ()) { | |
return withExtendedLifetime(arg0) { withExtendedLifetimes(arg1, arg2, f) } | |
} | |
// ... however more you want ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment