<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I may be missing something, but I don’t understand how to get the behavior of dispatch_once() without a bunch more code in cases in which I was using it to initialize “lazy-ish" instance variables.<div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> PlatonicPieceOfFurniture {</span></div></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">&nbsp; &nbsp; internal</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> modelDirectoryURL: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">URL</span></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class=""><br class=""></span></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">&nbsp; &nbsp; /* … */</span></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> floorGeometryAndMaterial: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">FloorPlatonicGeometryAndMaterial</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;dispatch_once(&amp;dispatchOnceLoadGeometriesAndMaterials) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.floorGeometryAndMaterialBacking =&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">try</span><span style="font-variant-ligatures: no-common-ligatures;" class="">! FloorPlatonicGeometryAndMaterial(modelDirectory:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.modelDirectoryURL)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> floorGeometryAndMaterialBacking!</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">private</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> floorGeometryAndMaterialBacking: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">FloorPlatonicGeometryAndMaterial</span><span style="font-variant-ligatures: no-common-ligatures;" class="">?</span></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">private</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;dispatchOnceLoadGeometriesAndMaterials:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">dispatch_once_t</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;&nbsp;=&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">0</span></div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class="">Note that ‘floorGeometryAndMaterial' isn’t a global, and that it requires ‘<span style="font-variant-ligatures: no-common-ligatures" class=""></span>modelDirectoryURL’ as input when it is lazily initialized, so I can't just do:</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="color: rgb(187, 44, 162);" class="">public</span>&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">lazy</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> floorGeometryAndMaterial: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">FloorPlatonicGeometryAndMaterial</span><span style="font-variant-ligatures: no-common-ligatures" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class="">! </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">FloorPlatonicGeometryAndMaterial</span><span style="font-variant-ligatures: no-common-ligatures" class="">(modelDirectory: modelDirectoryURL)</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Because that throws a “instance member ‘</span>modelDirectoryURL’ cannot be used on type ‘PlatonicPieceOfFurniture’” error in Xcode. (Also, honestly, I don’t see much utility in lazy instance variables in Swift, since I can’t use any state from the current instance to initialize them, so they’re usually no better than static variables — I don’t think I’ve ever used them, despite trying a bunch.)</div><div class=""><br class=""></div><div class="">Also this needs to be thread-safe, and loading the backing is very slow so we really don’t want to ever accidentally do it twice (although it would be safe to do so in my code), so I can’t just check if&nbsp;<span style="font-variant-ligatures: no-common-ligatures;" class=""><font color="#bb2ca2" class="">‘</font>floorGeometryAndMaterialBacking’ is nil and load it up if it is.</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><br class=""></div></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class="">I could do an ugly version of this with semaphores and an extra flag, but it seems a lot cleaner with dispatch_once().</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class="">Am I missing something obvious? The Swift 3 converter completely mangled my code into a static case which didn’t compile at all and couldn’t possibly work.</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class="">-Wil Shipley</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class="">Delicious Monster</div></body></html>