<div dir="ltr">How many of these use cases would be safely addressed if two Enum types could be unioned to form a new type?<div><br></div><div>It could use syntax similar to what is being proposed for existentials: <font face="monospace, monospace">(A|B)</font>, or something like this:</div><div><br></div><div><font face="monospace, monospace">enum C: A, B {}</font></div><div><br></div><div>Swift could generate code like this:<div><br></div><div><font face="monospace, monospace">enum A {</font></div><div><font face="monospace, monospace">  case A1, A2</font></div><div><font face="monospace, monospace">}</font></div><div><div><font face="monospace, monospace">enum B {</font></div><div><font face="monospace, monospace">  case B1, B2</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace">enum C {</font></div><div><font face="monospace, monospace">  case A(Module.A)</font></div><div><font face="monospace, monospace">  case B(Module.B)<br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  init(_ a: Module.A) { self = .A(a) }</font></div><div><font face="monospace, monospace">  init(_ b: Module.B) { self = .B(b)  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  static let A1 = C(A.A1)</font></div><div><font face="monospace, monospace">  static let A2 = C(A.A2)<br></font></div><div><div><font face="monospace, monospace">  static let B1 = C(B.B1)</font></div><div><font face="monospace, monospace">  static let B2 = C(B.B2)</font></div></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace">extension A {</font></div><div><font face="monospace, monospace">  init?(_ c: C) {</font></div><div><font face="monospace, monospace">    guard let case .A(a) = c else { return nil }</font></div><div><font face="monospace, monospace">    self = a</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace">}</font></div><div><div><font face="monospace, monospace">extension B {</font></div><div><font face="monospace, monospace">  init?(_ c: C) {</font></div><div><div><font face="monospace, monospace">    guard let case .B(b) = c else { return nil }</font></div><div><font face="monospace, monospace">    self = b</font></div></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace">}</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div>If I remember correctly there was already some proposals like this, they are probably more thought out than this suggestion.  I know I&#39;d find that useful, I don&#39;t think I&#39;d want the exhaustibility implications of extending an enum in another module.</div><div><br></div><div><br>On Friday, 1 July 2016, Marc Palmer via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><div><span style="background-color:rgba(255,255,255,0)">Hi,</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">I too groan when faced with the lack of extensibility on enums. As a potential framework writer, I&#39;d like to be able to use an enum as a key to dictionaries, supplying a minimum set of such enum cases, but allowing app developers to add new ones they require. </span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">Having read the proposal however, I have a major concern and question the entire idea. </span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">Given that there is unlikely to be a sane way to order the extended enum cases supplied by other modules, we will never be able to rely on the automatic ordinal values applied, nor their relative position in the natural sequence, for there isn&#39;t one outside of the first set of cases in the original definition. </span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">For many cases this may be fine, on the understanding that everything would have to compile from source, but my understanding is that we don&#39;t want that in future with ABI around the corner. Binary libraries would probably need to bake in the value of e.g. Int enum cases. (I think?)</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">I fear that if this proposal were implemented without some major restrictions (such as never allowing use of rawValue), we would regret it and suffer for example having to explicitly set enum case Int raw values for every case in these enums in every module always, and suffer compilation errors when other (maybe binary) modules change their explicit raw values and clash with other modules. It could be a dependency nightmare.</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">Essentially consigning extensible enums to never being useful for serialising their raw values seems of limited use to me, as often you may not know you need them to have unmoving raw values until it is too late and your code is in the wild. </span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">Perhaps I am missing some secret sauce?</span></div><div><span style="background-color:rgba(255,255,255,0)"><br>--</span><div><span style="background-color:rgba(255,255,255,0)">Marc Palmer</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div></div><blockquote type="cite"><font color="#000000"><span style="background-color:rgba(255,255,255,0)"></span></font></blockquote></div></div></blockquote></div>
</div></div>