[swift-evolution] Lazy var and deinit

Vladimir.S svabox at gmail.com
Thu Apr 21 08:12:03 CDT 2016


Seems to solve this problem I can suggest only this ugly solution:

class A {
     lazy var _b = B()
     var _b_used = false

     var b : B {
         get {_b_used = true; return _b }
         set {_b_used = true; _b = newValue }
     }

     deinit {
         print("deinit A")

         if _b_used {
             b.clean()
         }
     }

}

Better solution?

On 21.04.2016 13:27, Alexandr.moq via swift-evolution wrote:
> Should SWIFT initialize a variable in deinit method if it has not been initialized?
>
> For example:
> ```swift
> class A {
> 	lazy var b = B()
> 	deinit {
> 		b.clean()
> 	}
> }
> var a = A()
> a.b.doSomething() //1: variable was created
> a = A() //2: "clean" method was called for "b" variable
> a = A() //3: instance of A from step 2 should killed and "deinit" method is called. In this method "b" variable will be created, "clean" will be called and "b" will be killed. So, is it ok or better if swift doesn’t create lazy variables in deinit if variable is not created yet
> ```
> To be honest, I don’t know which topic I should use. Because I don’t know, it’s propose, bug or something else.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>


More information about the swift-evolution mailing list