devxoul
10/5/2015 - 4:49 PM

Experimental weakify and strongify in Swift using Generic.

Experimental weakify and strongify in Swift using Generic.

// The MIT License (MIT)
//
// Copyright (c) 2015 Suyeol Jeon (xoul.kr)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


/// Experimental weakify and strongify in Swift using Generic.
///
/// Example usage:
///
///     cell.didSelectButton = weakify(self) { `self`, button in
///         self.presentViewController(...)
///     }
///
/// is equal to:
///
///     cell.didSelectButton = { [weak self] button in
///         guard let `self` = self else {
///             return
///         }
///         self.presentViewController(...)
///     }
func weakify<Owner: AnyObject, Arg1>(owner: Owner, _ closure: (Owner, Arg1) -> Void) -> (Arg1 -> Void) {
    return { [weak owner] arg1 in
        if let owner = owner {
            closure(owner, arg1)
        }
    }
}

func weakify<Owner: AnyObject, Arg1, Arg2>(owner: Owner, _ closure: (Owner, Arg1, Arg2) -> Void) -> ((Arg1, Arg2) -> Void) {
    return { [weak owner] arg1, arg2 in
        if let owner = owner {
            closure(owner, arg1, arg2)
        }
    }
}

func weakify<Owner: AnyObject, Arg1, Arg2, Arg3>(owner: Owner, _ closure: (Owner, Arg1, Arg2, Arg3) -> Void) -> ((Arg1, Arg2, Arg3) -> Void) {
    return { [weak owner] arg1, arg2, arg3 in
        if let owner = owner {
            closure(owner, arg1, arg2, arg3)
        }
    }
}

func weakify<Owner: AnyObject, Arg1, Arg2, Arg3, Arg4>(owner: Owner, _ closure: (Owner, Arg1, Arg2, Arg3, Arg4) -> Void) -> ((Arg1, Arg2, Arg3, Arg4) -> Void) {
    return { [weak owner] arg1, arg2, arg3, arg4 in
        if let owner = owner {
            closure(owner, arg1, arg2, arg3, arg4)
        }
    }
}

// TODO: Arg5, Arg6, Arg7, ...


// MARK: - Test Compilable

struct Closure {
    var arg0: (() -> Void)?
    var arg1: (Int -> Void)?
    var arg2: ((Int, Int) -> Void)?
    var arg3: ((Int, Int, Int) -> Void)?
}


class Foo {
    func bar() {
        var closure = Closure()
        closure.arg0 = weakify(self) { `self` in }
        closure.arg1 = weakify(self) { `self`, a in }
        closure.arg2 = weakify(self) { `self`, a, b in }
        closure.arg3 = weakify(self) { `self`, a, b, c in }
    }
}

Foo().bar()