Lazy var and deinit


(Alexandr.moq) #1

Should SWIFT initialize a variable in deinit method if it has not been initialized?

For example:

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.


(Vladimir) #2

For exactly this example, from my point of view, b.clean() is actually using of the "b" property. Compiler, and I, don't know what '.clean()' means and actually it is the same as

deinit {
   b.doSomethingCool() // << shouldn't "b" created here to process your request?
}

But probably I understand the problem you are trying to solve.
How to check if lazy property/variable was actually initialized, so you can call its ".clean" only when it was really created and used. I.e. we just don't need to call b.clean if "b" was not even called and so was not created.

Right now I can't find a way to check this, probably I'm missing something..
So the question is : how to check if b was or was not actually "created" :
(something like)

deinit {
  if let b {
    b.clean()
  }
}

Anyone?

···

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:

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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Vladimir) #3

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:

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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution