<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"><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=""><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 &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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=""> &nbsp;&nbsp;&nbsp;case Number(Double)<br class=""> &nbsp;&nbsp;&nbsp;case Variable(String)<br class=""> &nbsp;&nbsp;&nbsp;indirect case Unary(Operator, Expression)<br class=""> &nbsp;&nbsp;&nbsp;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=""> &nbsp;&nbsp;&nbsp;case Number(Double)<br class=""> &nbsp;&nbsp;&nbsp;case Variable(String)<br class=""> &nbsp;&nbsp;&nbsp;indirect case Unary(Operator, Expression)<br class=""> &nbsp;&nbsp;&nbsp;indirect case Binary(Operator, Expression, Expression)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;var location: Int = 0<br class=""> &nbsp;&nbsp;&nbsp;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=""> &nbsp;var type: SomethingType<br class=""> &nbsp;var width: Int<br class=""> &nbsp;var height: Int<br class="">}<br class=""><br class="">enum SomethingType {<br class=""> &nbsp;case TypeA<br class=""> &nbsp;case TypeB<br class="">}<br class=""><br class="">You would just have:<br class=""><br class="">enum Something {<br class=""> &nbsp;case TypeA<br class=""> &nbsp;case TypeB<br class=""><br class=""> &nbsp;var width: Int<br class=""> &nbsp;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=""></body></html>