<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="">Hard to say without full code, but the following code compiles just fine in Xcode 8:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> FloorPlatonicGeometryAndMaterial {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(modelDirectory: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSURL</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">/// ...</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" 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; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><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 style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">internal</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=""> modelDirectoryURL: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">URL</span></div><p style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> floorGeometryAndMaterial: </span><span style="font-variant-ligatures: no-common-ligatures" class="">FloorPlatonicGeometryAndMaterial</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">floorGeometryAndMaterialBacking</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">!</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><p style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">lazy</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> floorGeometryAndMaterialBacking: </span><span style="font-variant-ligatures: no-common-ligatures" class="">FloorPlatonicGeometryAndMaterial</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? = </span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><span class="Apple-tab-span" style="white-space:pre">                                </span>try</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">! </span><span style="font-variant-ligatures: no-common-ligatures" class="">FloorPlatonicGeometryAndMaterial</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(modelDirectory: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">modelDirectoryURL</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><p style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(modelDirectoryURL: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">URL</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">modelDirectoryURL</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = modelDirectoryURL</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">/// ...</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" 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; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 10px;" class="">All lazy initialization pretty much uses dispatch_once. Also remember, that your code can be as followed:</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 10px;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class="">private lazy var stringValue: String? = {</div><div style="margin: 0px; line-height: normal;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var str = self.description</div><div style="margin: 0px; line-height: normal;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>str += "\n"</div><div style="margin: 0px; line-height: normal;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>...</div><div style="margin: 0px; line-height: normal;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return str</div><div style="margin: 0px; line-height: normal;" class="">}()</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">lazy var initialization doesn't have to be a one-liner, but can be an applied closure.</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 16, 2016, at 7:48 PM, William Shipley via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div 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=""> 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=""> /* … */</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=""> </span><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="">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=""> dispatch_once(&dispatchOnceLoadGeometriesAndMaterials) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </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 = </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: </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=""> </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=""> }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </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=""> </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=""> dispatchOnceLoadGeometriesAndMaterials: </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=""> = </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=""> </span><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="color: rgb(187, 44, 162);" class="">public</span> </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 <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></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>