<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I really like this idea. Spatially moving cleanup next to unsafe operations is good practice.</div><div class=""><br class=""></div><div class="">In normal code, I want my cleanup to follow as closely as possible to my unsafe act:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">let buffer: UnsafeMutablePointer&lt;CChar&gt; = UnsafeMutablePointer(allocatingCapacity: chunkSize)</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; defer { buffer.deallocateCapacity(chunkSize) }</font></div></div><div class=""><br class=""></div><div class="">(Sorry for the horrible example, but it's the best I could grep up with on a moment's notice)</div><div class=""><br class=""></div><div class="">I like your idea but what I want to see is not the deinit child closure in init you propose but a new keyword that means defer-on-deinit-cleanup</div><div class=""><br class=""></div><div class=""><div dir="auto" class=""><div class=""><span class="" style="background-color: rgba(255, 255, 255, 0);"><font face="Menlo" class="">self.ptr = UnsafeMutablePointer&lt;T&gt;(allocatingCapacity: count)</font></span></div><div class=""><span style="font-family: Menlo;" class="">&nbsp; &nbsp; deferringDeInit { self.ptr.deallocateCapacity(count) }</span></div></div></div><div class=""><br class=""></div><div class="">Or something.</div><div class=""><br class=""></div><div class="">-- E</div><div class="">p.s. Normally I put them on the same line with a semicolon but dang these things can be long</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Jun 8, 2016, at 10:54 AM, Graham Perks via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Teach init a 'defer'-like ability to deinit<br class=""><br class="">'defer' is a great way to ensure some clean up code is run; it's declaritive locality to the resource acquisition is a boon to clarity.<br class=""><br class="">Swift offers no support for resources acquired during 'init'.<br class=""><br class="">For an example, from&nbsp;<a dir="ltr" href="https://www.mikeash.com/pyblog/friday-qa-2015-04-17-lets-build-swiftarray.html" x-apple-data-detectors="true" x-apple-data-detectors-type="link" x-apple-data-detectors-result="0" class="">https://www.mikeash.com/pyblog/friday-qa-2015-04-17-lets-build-swiftarray.html</a><br class=""><br class="">init(count: Int = 0, ptr: UnsafeMutablePointer&lt;T&gt; = nil) {<br class="">&nbsp; &nbsp; self.count = count<br class="">&nbsp; &nbsp; self.space = count<br class=""><br class="">&nbsp; &nbsp; self.ptr = UnsafeMutablePointer&lt;T&gt;.alloc(count)<br class="">&nbsp; &nbsp; self.ptr.initializeFrom(ptr, count: count)<br class="">}<br class=""><br class="">deinit {<br class="">&nbsp; &nbsp; ptr.destroy(...)<br class="">&nbsp; &nbsp; ptr.dealloc(...)<br class="">}<br class=""><br class="">Another 'resource' might be adding an NSNotificationCenter observer, and wanting to unobserve in deinit (no need in OS X 10.11, iOS 9, but for earlier releases this is a valid example).<br class=""><br class="">Changing the above code to use a 'defer' style deinit block might look like:<br class=""><br class="">init(count: Int = 0, ptr: UnsafeMutablePointer&lt;T&gt; = nil) {<br class="">&nbsp; &nbsp; self.count = count<br class="">&nbsp; &nbsp; self.space = count<br class=""><br class="">&nbsp; &nbsp; self.ptr = UnsafeMutablePointer&lt;T&gt;.alloc(count)<br class="">&nbsp; &nbsp; self.ptr.initializeFrom(ptr, count: count)<br class=""><br class="">&nbsp; &nbsp; deinit {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; ptr.destroy(...)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; ptr.dealloc(...)<br class="">&nbsp; &nbsp; }<br class=""><br class="">&nbsp; &nbsp; // NSNotificationCenter example too<br class="">&nbsp; &nbsp; NSNotificationCenter.defaultCenter().addObserver(...)<br class="">&nbsp; &nbsp; deinit {&nbsp;<br class="">&nbsp; &nbsp; &nbsp; &nbsp; NSNotificationCenter.defaultCenter().removeObserver(...)<br class="">&nbsp; &nbsp; }<br class="">}<br class=""><br class="">The need to provide a separate implemention of deinit is gone. Reasoning for 'defer' applies here. There is good locality between what was initialized and what needs cleaning up.<br class=""><br class="">Considerations:<br class="">1. Should deinit blocks be invoked before or after code in an explicit deinit method?<br class="">2. Should deinit blocks be allowed in other methods; e.g. viewDidLoad()?<br class="">3. How should deinit blocks be prevented from strongly capturing self (thus preventing themselves from ever running!)?<br class=""></span></div></div></div></blockquote></div><br class=""><div class=""><br class=""></div></body></html>