Re: [swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-12 Thread Charlie Monroe via swift-evolution
> On Jun 11, 2016, at 5:18 AM, Xiaodi Wu via swift-evolution > wrote: > > On Fri, Jun 10, 2016 at 9:55 PM, Jonathan Hull via swift-evolution > mailto:swift-evolution@swift.org>> wrote: > I really love this idea. My mental model of it is that it is exactly like > ‘defer’, except it works on t

Re: [swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-11 Thread Brent Royal-Gordon via swift-evolution
> class Foo { > var ptr: UnsafeMutablePointer { > deinit { > ptr.destroy(...) > ptr.dealloc(...) > } > } > > init(count: Int = 0, ptr: UnsafeMutablePointer = nil) { > self.count = count > self.space = count > > self.ptr = UnsafeMutablePointer.alloc(count) >

Re: [swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-10 Thread Patrick Smith via swift-evolution
I like the idea, I don’t like the potential to capture anything else though, or for instances to store closure references. It should offer similar performance to a standard `deinit`. Instead, what about a sibling to `willSet`, `didSet` etc? class Foo { var ptr: UnsafeMutablePointer { dein

Re: [swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-10 Thread Xiaodi Wu via swift-evolution
On Fri, Jun 10, 2016 at 9:55 PM, Jonathan Hull via swift-evolution < swift-evolution@swift.org> wrote: > I really love this idea. My mental model of it is that it is exactly like > ‘defer’, except it works on the lifetime of the object instance instead of > a function/method. Same thing, differe

Re: [swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-10 Thread Jonathan Hull via swift-evolution
I really love this idea. My mental model of it is that it is exactly like ‘defer’, except it works on the lifetime of the object instance instead of a function/method. Same thing, different scope. I like how the creation and destruction are right next to one another. It also solves a lot of

Re: [swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-10 Thread Erica Sadun via swift-evolution
Twitter tl;dr: > Brent: So each instance must remember which init was used for it and then run > the matching deinit code at deinit time? > Me: In my version, the constructive act and destructive act are always > paired, even redundantly, using a stack if needed > Graham: so all your deferredDei

Re: [swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-08 Thread Erica Sadun via swift-evolution
I really like this idea. Spatially moving cleanup next to unsafe operations is good practice. In normal code, I want my cleanup to follow as closely as possible to my unsafe act: let buffer: UnsafeMutablePointer = UnsafeMutablePointer(allocatingCapacity: chunkSize) defer { buffer.deallocat

[swift-evolution] Sketch: Teach init a 'defer'-like ability to deinit

2016-06-08 Thread Graham Perks via swift-evolution
Teach init a 'defer'-like ability to deinit '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. Swift offers no support for resources acquired during 'init'. For an example, from https://www.mikeash.com/pyblog/