<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><font face="Courier" class="">We sometimes encounter the situation that a server will add over time new values to existing `enums`.</font><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">e.g. in the `enum Animal` example `cat` gets added.</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">To not break existing clients, we often use something like this:</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class=""> enum Animal: Int, Codable {</font></div><div class=""><span style="font-family: Courier;" class=""> case unknown = 0</span></div><div class=""><span style="font-family: Courier;" class=""> case chicken = 1, dog, turkey, cow</span></div><div class=""><font face="Courier" class=""> }</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">and all cases which are unknown (like a new `cat` case) will map to `unknown` instead.</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">I could probably do this by manually implementing `init(from:)`:</font></div><div class=""><span class="Apple-tab-span" style="white-space:pre"><font face="Courier" class="">        </font></span></div><div class=""><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> public init(from decoder: Decoder) throws {</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> // Decodes as a single value; no keys.</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> let intValue = try decoder.singleValueContainer().decode(Int.self)</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> if let value = Self(rawValue: intValue) {</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> self = value</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> } else {</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> self = .unknown</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> }</font></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""> }</font></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""><br class=""></font></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class="">But maybe there is a better way envisioned by the authors of SE-0166?</font></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class=""><br class=""></font></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class="">Cheers</font></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font face="Courier" class="">Marc</font></span></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><br class=""></div></div></body></html>