Skip to content

Instantly share code, notes, and snippets.

@rnapier
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnapier/0d24ca74e4b4c5bfb3b4 to your computer and use it in GitHub Desktop.
Save rnapier/0d24ca74e4b4c5bfb3b4 to your computer and use it in GitHub Desktop.
withExtendedLifetimes (arbitrary number of parameters)
// 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