<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div> <br> <div><blockquote type="cite" class="clean_bq" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><span><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br>True indeed… but can we agree that this is just an hypothetic example, and no issue that is likely to happen in productive code?<div class="">Or is this actually taken from one of the projects you measured?</div></div></span></blockquote></div><p>Here is the expanded edition which is productive.</p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures">/**A dummy return value to indicate a method isn't threadsafe.</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures"> Because unused return values produce warnings in Swift, a caller who uses the method absentmindedly will have a warning in their code. This can be suppressed with `let _: NotThreadSafe` from the caller. */</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">struct</span><span style="font-variant-ligatures: no-common-ligatures"> NotThreadSafe { }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"></span><br></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures">///There are many cache implementations with different performance characteristics.</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures">///we model them with a protocol.</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">protocol</span><span style="font-variant-ligatures: no-common-ligatures"> CacheImplementation {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">func</span><span style="font-variant-ligatures: no-common-ligatures"> prune() -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">NotThreadSafe</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">}</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"></span><br></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures">///Many components share a single cache, so we expose it as a reference type</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">class</span><span style="font-variant-ligatures: no-common-ligatures"> Cache {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"> </span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">init</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">() { </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">preconditionFailure</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span><span style="font-variant-ligatures: no-common-ligatures">"Provide a constructor to satisfy the compiler"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">) }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">private</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> v: </span><span style="font-variant-ligatures: no-common-ligatures">CacheImplementation</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">private</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">let</span><span style="font-variant-ligatures: no-common-ligatures"> lock = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">DispatchQueue</span><span style="font-variant-ligatures: no-common-ligatures">(label: </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"lock"</span><span style="font-variant-ligatures: no-common-ligatures">)</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"> </span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"> </span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">private</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">func</span><span style="font-variant-ligatures: no-common-ligatures"> prune() -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">NotThreadSafe</span><span style="font-variant-ligatures: no-common-ligatures"> {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">v</span><span style="font-variant-ligatures: no-common-ligatures">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d">prune</span><span style="font-variant-ligatures: no-common-ligatures">()</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">NotThreadSafe</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">()</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">///expose a threadsafe API to callers</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">internal</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">func</span><span style="font-variant-ligatures: no-common-ligatures"> prune() {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">lock</span><span style="font-variant-ligatures: no-common-ligatures">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81">sync</span><span style="font-variant-ligatures: no-common-ligatures"> {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures">//supress the warning, since we have a lock.</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">let</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">_</span><span style="font-variant-ligatures: no-common-ligatures">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">NotThreadSafe</span><span style="font-variant-ligatures: no-common-ligatures"> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">self</span><span style="font-variant-ligatures: no-common-ligatures">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d">prune</span><span style="font-variant-ligatures: no-common-ligatures">()</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">}</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures"></span><br></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);"><span style="font-variant-ligatures: no-common-ligatures">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">Cache</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">func</span><span style="font-variant-ligatures: no-common-ligatures"> pruneAgressively() {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">for</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">_</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">in</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">0</span><span style="font-variant-ligatures: no-common-ligatures">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">5</span><span style="font-variant-ligatures: no-common-ligatures"> {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2">self</span><span style="font-variant-ligatures: no-common-ligatures">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d">prune</span><span style="font-variant-ligatures: no-common-ligatures">()</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">
</p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures">}</span></p><div><span style="font-variant-ligatures: no-common-ligatures"><br></span></div><p>Note that, in this example, the scoped access keyword actually makes extensions *easier*, not harder, to write, because the extension does not need to choose between a safe and unsafe version of the method.</p><div></div></body></html>