[swift-evolution] A compiler option to warn if a closure captures a strong reference to a class instance?

Lauri Lehmijoki lauri.lehmijoki at iki.fi
Mon Feb 20 05:22:39 CST 2017

I'm developing an application where we use RxSwift heavily. RxSwift is a
stream library. Consequently, closures that we pass to its combinators
often live infinitely (this is because one can use RxSwift to represent
infinitely long sequences in time).

Closures with infinite lifespan have implications for the question "what is
the best reference capture mode for closures". My experience is that in
RxSwift applications, the current default (strong) is almost always
suboptimal. It leads to difficult-to-detect memory leaks and introduces a
"gotcha" factor to programmers who are new to Swift. I'd prefer the default
to be weak capture.

So, I'd like to ask you two things:

A) By default, why the Swift closure captures values strongly?
B) Should we add a compiler option that, when turned on, would emit a
warning if a closure strongly captures a class instance?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170220/200e7a8a/attachment.html>

More information about the swift-evolution mailing list