<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>None of the state-like nature of enums would be lost.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">enum Something {</div><div id="AppleMailSignature"> case StateA</div><div id="AppleMailSignature"> case StateB(Double)</div><div id="AppleMailSignature"> var prop: Int</div><div id="AppleMailSignature">}</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">Would simply be a another way to write:</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">enum Something {</div><div id="AppleMailSignature"> case StateA(Int)</div><div id="AppleMailSignature"> case StateB(Double, Int)</div><div id="AppleMailSignature">}</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">The properties would just be like associated values that are included for every case. Of course when there's only two cases and one property, it's not a problem. But if you have more cases and more common values, it quickly gets out of hand.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I don't really see how the containing struct solves the problem in the example, since Expression only has other Expressions as children. You could of course modify it to contain Captures instead, but that doesn't express the recursiveness of expressions in the same elegant way.<br><br>Best,</div><div id="AppleMailSignature">Jonathan</div><div><br>9 dec. 2015 kl. 21:36 skrev Tommy van der Vorst <<a href="mailto:tommy@pixelspark.nl">tommy@pixelspark.nl</a>>:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><meta http-equiv="Content-Type" content="text/html charset=utf-8"><div class="">Hi Jonathan,</div><div class=""><br class=""></div><div class="">One of the great things about enums with attached values is that they define discrete 'states' in which the enum can be, and what values to expect in each state. If you want to add information to an enum value that is not related to its state, it sounds to me like you rather should use a struct for that (possibly embedding the 'state' part), like you say, i.e.:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">enum</span> Expression {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> Number(Double)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> Variable(String)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">indirect</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> Unary(Operator, Expression)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">indirect</span> <span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span> Binary(Operator, Expression, Expression)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">struct</span> Capture {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> location: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> length: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> expression: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Expression</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">_</span> expression: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Expression</span>, location: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Int</span>, length: <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Int</span>) {...}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span> expr = <span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">Capture</span>(.Number(3.0), location: .., length: ...)</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">In my view, adding independent stored properties to enum would make this not much easier to write or read, but only more confusion for people expecting the 'state machine' like behaviour of enum.</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Best,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Tommy.</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">Op 9 dec. 2015, om 21:28 heeft Jonathan Hise Kaldma via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> het volgende geschreven:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi everyone,<br class=""><br class="">Being able to associate values with enums is one of the features I love about Swift, especially together with the ability to make enums contain themselves. It makes it really easy to model tree-like structures, e.g. arithmetic expressions:<br class=""><br class="">enum Expression {<br class=""> case Number(Double)<br class=""> case Variable(String)<br class=""> indirect case Unary(Operator, Expression)<br class=""> indirect case Binary(Operator, Expression, Expression)<br class="">}<br class=""><br class="">This works great with Swift’s switch statement and let binding, and cuts out a lot of unnecessary code. But it breaks down if you need to keep track of more data for each tree node, e.g. if you’re making a parser and need to keep track of source location. This could easily be solved with stored properties:<br class=""><br class="">enum Expression {<br class=""> case Number(Double)<br class=""> case Variable(String)<br class=""> indirect case Unary(Operator, Expression)<br class=""> indirect case Binary(Operator, Expression, Expression)<br class=""><br class=""> var location: Int = 0<br class=""> var length: Int = 0<br class="">}<br class=""><br class="">If the properties have default values, you would still get the regular enum initializer without properties like today:<br class=""><br class="">let expr = .Number(3)<br class=""><br class="">But you would also get a memberwise initializer with the properties after the associated values, which you can use if you the properties don’t have default values or if you just want to set them to something else.<br class=""><br class="">let expr = .Number(3, location: 5, length: 1)<br class=""><br class="">Other than that, enums would work just like they do today. Aside from the previous example, I think this would simplify a lot of use cases where you have a struct and an accompanying enum, so rather than:<br class=""><br class="">struct Something {<br class=""> var type: SomethingType<br class=""> var width: Int<br class=""> var height: Int<br class="">}<br class=""><br class="">enum SomethingType {<br class=""> case TypeA<br class=""> case TypeB<br class="">}<br class=""><br class="">You would just have:<br class=""><br class="">enum Something {<br class=""> case TypeA<br class=""> case TypeB<br class=""><br class=""> var width: Int<br class=""> var length: Int<br class="">}<br class=""><br class="">Feels very Swifty to me.<br class=""><br class="">Thanks,<br class="">Jonathan<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></blockquote></body></html>