Last active
June 30, 2016 01:47
-
-
Save rhysforyou/7b4bf23290826b0eb50acd6383ab73fc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import UIKit | |
func unownedFunc<T : AnyObject>(target: T, method: (T) -> () -> ()) -> () -> () { | |
return { [unowned target] in return method(target)() } | |
} | |
func unownedFunc<T : AnyObject, R>(target: T, method: (T) -> () -> R) -> () -> R { | |
return { [unowned target] in return method(target)() } | |
} | |
func unownedFunc<T : AnyObject, A>(target: T, method: (T) -> (A) -> ()) -> (A) -> () { | |
return { [unowned target] a in return method(target)(a) } | |
} | |
func unownedFunc<T : AnyObject, R, A>(target: T, method: (T) -> (A) -> R) -> (A) -> R { | |
return { [unowned target] a in return method(target)(a) } | |
} | |
func unownedFunc<T : AnyObject, A, B>(target: T, method: (T) -> (A, B) -> ()) -> (A, B) -> () { | |
return { [unowned target] a, b in return method(target)(a, b) } | |
} | |
func unownedFunc<T : AnyObject, R, A, B>(target: T, method: (T) -> (A, B) -> R) -> (A, B) -> R { | |
return { [unowned target] a, b in return method(target)(a, b) } | |
} | |
func unownedFunc<T : AnyObject, A, B, C>(target: T, method: (T) -> (A, B, C) -> ()) -> (A, B, C) -> () { | |
return { [unowned target] a, b, c in return method(target)(a, b, c) } | |
} | |
func unownedFunc<T : AnyObject, R, A, B, C>(target: T, method: (T) -> (A, B, C) -> R) -> (A, B, C) -> R { | |
return { [unowned target] a, b, c in return method(target)(a, b, c) } | |
} | |
func unownedFunc<T : AnyObject, A, B, C, D>(target: T, method: (T) -> (A, B, C, D) -> ()) -> (A, B, C, D) -> () { | |
return { [unowned target] a, b, c, d in return method(target)(a, b, c, d) } | |
} | |
func unownedFunc<T : AnyObject, R, A, B, C, D>(target: T, method: (T) -> (A, B, C, D) -> R) -> (A, B, C, D) -> R { | |
return { [unowned target] a, b, c, d in return method(target)(a, b, c, d) } | |
} | |
func unownedFunc<T : AnyObject, A, B, C, D, E>(target: T, method: (T) -> (A, B, C, D, E) -> ()) -> (A, B, C, D, E) -> () { | |
return { [unowned target] a, b, c, d, e in return method(target)(a, b, c, d, e) } | |
} | |
func unownedFunc<T : AnyObject, R, A, B, C, D, E>(target: T, method: (T) -> (A, B, C, D, E) -> R) -> (A, B, C, D, E) -> R { | |
return { [unowned target] a, b, c, d, e in return method(target)(a, b, c, d, e) } | |
} | |
class Greeter { | |
func greet(person: String) -> String { | |
return "Hello, \(person)!" | |
} | |
} | |
let greeter = Greeter() | |
let people = ["Rhys", "Tom"] | |
// `greeter` is not retained | |
print(people.map(unownedFunc(greeter, method: Greeter.greet))) | |
// Output: | |
// ["Hello, Rhys!", "Hello, Tom!"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment