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

Mihael Hategan hategan at mcs.anl.gov
Mon May 25 12:13:01 CDT 2015


Hi,

This is a mailing list for swift-lang.org rather than
developer.apple.com/swift/, so an entirely different language. I would
suggest posting your question in a more appropriate place.

I'll take a stab at answering your question though: you have multiple
instances of the testLabel field (one for each instance of UILabel).
Each of them is initialized at most once (although due to the loop, none
of them get to be initialized).

Mihael

On Mon, 2015-05-25 at 12:34 +0900, fullc0de wrote:
> 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.
> _______________________________________________
> Swift-user mailing list
> Swift-user at ci.uchicago.edu
> https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-user





More information about the Swift-user mailing list