<div dir="ltr">I really like the idea behind this proposal.<div><br></div><div>Some questions:</div><div><br></div><div>- Would the enum &#39;slice&#39; be a distinct type relative to the base enum?</div><div>- On a related note, would shared cases between the sliced enum and the base enum be implicitly convertible?</div><div>- If they aren&#39;t implicitly convertible between each other, would there be an affordance to perform conversions (e.g. a &quot;parentEnumType&quot; property and an &quot;init?(parentEnumType:)&quot; initializer)/</div><div>- Would you be able to further slice a sliced enum? If so, would they share the same parent, or would the &#39;parent&#39; of the sliced sliced enum be the sliced enum?</div><div>- If the parent enum has members that switch on &#39;self&#39;, would those members be available to the child automatically?</div><div>- What happens if you have two (or more) slices with disjoint members? (e.g. &#39;LCDColors&#39; and &#39;TrafficLightColors&#39;) Would they be considered completely separate &#39;sub-types&#39;?</div><div><br></div><div>Best,</div><div>Austin</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 3, 2016 at 6:22 AM, T.J. Usiyan via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>This is loosely related to but not meant to &#39;compete&#39; with the ad hoc enum proposal.</div><div><br></div><div>## Introduction</div><div><br></div><div>This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.</div><div><br></div><div>Swift-evolution thread: [Discussion thread topic for that proposal](<a href="http://news.gmane.org/gmane.comp.lang.swift.evolution" target="_blank">http://news.gmane.org/gmane.comp.lang.swift.evolution</a>)</div><div><br></div><div>## Motivation</div><div>Consider a situation where we have an enum `Colors` which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum `LCDColorModel` with only three colors, `red, blue, green` .</div><div><br></div><div>``` swift</div><div>enum Colors {</div><div><span style="white-space:pre-wrap">        </span>case red, orange, yellow, green, blue, indigo, violet</div><div><span style="white-space:pre-wrap">        </span>…</div><div>}</div><div><br></div><div>enum LCDColors {</div><div><span style="white-space:pre-wrap">        </span>case red, green, blue</div><div>}</div><div>```</div><div><br></div><div>The cases in `LCDColors` in our scenario do not require different behavior from their similarly named cases in `Colors`. We would like, simply stated, to explicitly restrict the cases allowed within a specific portion of our software. There are, currently, a few approaches </div><div><span style="white-space:pre-wrap">        </span>1. Duplicate functionality in `LCDColors` </div><div><span style="white-space:pre-wrap">                </span>- Completely manually</div><div><span style="white-space:pre-wrap">                </span>- Protocols with &#39;minimal&#39; manual duplication</div><div><span style="white-space:pre-wrap">        </span>2. Avoid duplication by allowing conversion to `Colors`. </div><div><br></div><div>Neither of these solutions make the subset relationship between `Colors` and `LCDColors`  clear or strict.</div><div><br></div><div>## Proposed solution</div><div><br></div><div>Add syntax to describe a restricted set of cases from an enum. </div><div><br></div><div>```swift</div><div>typealias LCDColors = Colors.(red|green|blue)</div><div>```</div><div><br></div><div>`LCDColors ` has all of the type and instance methods of `Colors`. Cases must appear in the same order as their original declaration. </div><div><br></div><div><br></div><div>## Detailed design</div><div><br></div><div>While I am unsure of the entirety of the design, I propose that name mangling be used which, along with the declaration order restriction should mean that all possible subsets have a stable and predictable name which contains all of the information necessary to infer cases. </div><div><br></div><div>## Impact on existing code</div><div><br></div><div>This is an additive change which should have no breaking change to existing code.</div><div><br></div><div><br></div></div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>