> On Jun 16, 2016, at 11:32 AM, Charlie Monroe via swift-evolution 
> <[email protected]> wrote:
> 
> Hard to say without full code, but the following code compiles just fine in 
> Xcode 8:
> 
> class FloorPlatonicGeometryAndMaterial {
>       init(modelDirectory: NSURL) throws {
>               /// ...
>       }
> }
> 
> class PlatonicPieceOfFurniture {
>       internal var modelDirectoryURL: URL
>       
>       var floorGeometryAndMaterial: FloorPlatonicGeometryAndMaterial {
>               return self.floorGeometryAndMaterialBacking!
>       }
>       
>       private lazy var floorGeometryAndMaterialBacking: 
> FloorPlatonicGeometryAndMaterial? = 
>                               try! 
> FloorPlatonicGeometryAndMaterial(modelDirectory: self.modelDirectoryURL)
>       
>       init(modelDirectoryURL: URL) {
>               self.modelDirectoryURL = modelDirectoryURL
>               /// ...
>       }
> }
> 
> All lazy initialization pretty much uses dispatch_once. Also remember, that 
> your code can be as followed:
> 
> private lazy var stringValue: String? = {
>       var str = self.description
>       str += "\n"
>       ...
>       return str
> }()
> 
> lazy var initialization doesn't have to be a one-liner, but can be an applied 
> closure.

`lazy var` does *not* use dispatch_once, so it isn't thread-safe without 
synchronization. Only global and static properties are initialized with a 
dispatch_once-like mechanism.

-Joe
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to