Add something like [unowned self] syntax for passing instance methods into closure parameters without creating retain cycles

(Nick Abalov) #1

In swift it is possible to pass instance method into function parameter
that takes closure. But while closure allows to make self unowned, passing
instance method does not provide syntax to make self unowned, while
instance method creates strong self reference, those creating retain cycle.
This leads to leaks that are not obvious at first glance, and some might be
very small and be left unnoticed for some time resulting in hard debugging

For example:


creates retain cycle, which is easy mistake to make and is not obvious at
first look, and there is no syntax to make self unowned in this case. One
has to wrap method call into closure as follows:

        self.selectedPlayer.asObservable().subscribeNext {[unowned self]
(player) in



See also related StackOverflow question at

I think it would be a good idea to either provide a syntax to mark self as
unowned when passing instance method into closure parameter, or to make
self unowned implicitly when method is passed instead of closure.
Of course "Explicit is better than implicit" if there is any case for
passing method with strong self reference, so some syntax might be
introduced to mark unowned or strong and warning (or error) be raised if
method is passed without that mark.