[Swift-user] Lazy Initializer doesn't defend against calling twice.

fullc0de fullc0de at gmail.com
Sun May 24 22:34:07 CDT 2015


Hello, folks.

I have found a case that lazy initializer is called twice when the
initializer is placed in a recursion.
In this case, I don't have any thought whether it is allowed to be called
twice or not. Until now, I have known that lazy keyword guarantees being
worked only once. Is it misunderstanding?

Test code is the following that:

    lazy var testLabel: UILabel = {
>         println("testLabel self = \(self)")
>         let label = UILabel()
>         label.text = "hello"
>         self.testLabel.text = "world"
>         return label
>         }()


As you know, this code is really nonsense. But, I want to test if lazy
guarantees being worked only once or not in the recursion. From this code,
I could meet an infinite recursion.

Isn't this case included in lazy's guarantee that doing once?


Best regards.

Kyokook Hwang.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-user/attachments/20150525/d21d533c/attachment.html>


More information about the Swift-user mailing list