<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=""><br class=""><div><blockquote type="cite" class=""><div class="">Le 3 août 2017 à 02:09, Jordan Rose via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">P.S. There's a reason why Decodable uses an initializer instead of a factory-like method on the type but I can't remember what it is right now. I think it's something to do with having the right result type, which would have to be either 'Any' or an associated type if it wasn't just 'Self'. (And if it is 'Self' then it has all the same problems as an initializer and would require extra syntax.) Itai would know for sure.</div></div></blockquote><br class=""></div><div>For anyone interested, factory methods *can* retroactivaly be added to existing classes. This is how the SQLite library GRDB.swift is able to decode classes hierarchies like NSString, NSNumber, NSDecimalNumber, etc. from SQLite values:</div><div><br class=""></div><div>The protocol for types that can instantiate from SQLite values has a factory method:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class=""> public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> DatabaseValueConvertible</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 121, 164);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">/// Returns a value initialized from *dbValue*, if possible.</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">static</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> fromDatabaseValue(</span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> dbValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">DatabaseValue</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c59a7" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div>Having Foundation classes implement it uses various techniques:</div><div><br class=""></div><div>1. "Casting" (Data to NSData, or NSDate to Date, depending on which type provides the root conformance)</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Workaround Swift inconvenience around factory methods of non-final classes</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> cast<T, U>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> value: </span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">? {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> value </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">as</span><span style="font-variant-ligatures: no-common-ligatures" class="">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">U</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 121, 164);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 89, 167);" class="">NSData</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> : </span><span style="color: rgb(32, 128, 159); font-variant-ligatures: no-common-ligatures;" class="">DatabaseValueConvertible</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> fromDatabaseValue(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> dbValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(32, 128, 159);" class="">DatabaseValue</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 89, 167);" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? {</span></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Use Data conformance</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> data = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c59a7" class="">Data</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">fromDatabaseValue</span><span style="font-variant-ligatures: no-common-ligatures" class="">(dbValue) </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">cast</span><span style="font-variant-ligatures: no-common-ligatures" class="">(data)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">// Derives Date conformance from NSDate, for example</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c59a7" class="">ReferenceConvertible</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">: DatabaseValueConvertible, </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.ReferenceType: DatabaseValueConvertible {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 121, 164);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> fromDatabaseValue(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> dbValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(32, 128, 159);" class="">DatabaseValue</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(32, 128, 159);" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> ReferenceType.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(32, 128, 159);" class="">fromDatabaseValue</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(dbValue).</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 89, 167);" class="">flatMap</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(32, 128, 159);" class="">cast</span><span style="font-variant-ligatures: no-common-ligatures;" class="">($0) }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><br class=""></div><div class="">2. Using magic Foundation initializers (magic because the code below compiles even if those are not *required* initializers). Works for NSNumber, NSDecimalNumber, NSString:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(32, 128, 159);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c59a7" class="">NSNumber</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures" class="">DatabaseValueConvertible</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="color: rgb(61, 121, 164); font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">public</span><span style="color: rgb(61, 121, 164); font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">static</span><span style="color: rgb(61, 121, 164); font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">func</span><span style="color: rgb(61, 121, 164); font-variant-ligatures: no-common-ligatures;" class=""> fromDatabaseValue(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">_</span><span style="color: rgb(61, 121, 164); font-variant-ligatures: no-common-ligatures;" class=""> dbValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(32, 128, 159);" class="">DatabaseValue</span><span style="color: rgb(61, 121, 164); font-variant-ligatures: no-common-ligatures;" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 89, 167);" class="">Self</span><span style="color: rgb(61, 121, 164); font-variant-ligatures: no-common-ligatures;" class="">? {</span></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> dbValue.</span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">storage</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">int64</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> int64):</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(value: int64)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">double</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> double):</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(value: double)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">default</span><span style="font-variant-ligatures: no-common-ligatures" class="">:</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(32, 128, 159);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c59a7" class="">NSString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures" class="">DatabaseValueConvertible</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 121, 164);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> fromDatabaseValue(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(129, 53, 160);" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> dbValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(32, 128, 159);" class="">DatabaseValue</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 89, 167);" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Use String conformance</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> string = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c59a7" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #20809f" class="">fromDatabaseValue</span><span style="font-variant-ligatures: no-common-ligatures" class="">(dbValue) </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #8135a0" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(string: string)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div></div><div class=""><br class=""></div><div class="">The magic about Foundation initializers above makes me doubt that this technique is general enough for Decodable to profit from it, though. Yes it runs on Linux, so I'm not even sure if objc runtime is required or not. I'm clueless ???????</div><div class=""><br class=""></div><div class="">Gwendal Roué</div><div class=""><br class=""></div></div></body></html>