<html><head><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="">A use case for stored properties in enums would be improvements to ErrorType.<div class=""><br class=""></div><div class="">Today a struct is a common practical solution for defining an ErrorType with shared properties between error cases. And this removes the ability to use the nice pattern matching in catch clauses.</div><div class=""><br class=""></div><div class="">Gwendal Roué</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">Le 9 déc. 2015 à 22:06, Dave DeLong via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I came across the <i class="">exact</i> same problem a couple of months ago (expressions with source ranges).</div><div class=""><br class=""></div><div class="">The issue with the struct-and-enum approach is that it increases the amount of indirection you have in order to get at the data you care about. When dealing with expressions, you almost always care about the expression type (i.e., the enum value). Very rarely do you need the range of the original expression in the source string. However, the struct-and-enum approach optimizes for the accessing range, but not the type and its associated values.</div><div class=""><br class=""></div><div class="">I would be very much in favor of seeing stored properties on enums.</div><div class=""><br class=""></div><div class="">Dave</div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 9, 2015, at 1:36 PM, Tommy van der Vorst via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div 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 <<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="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=xwL-2FBGgqPlrQ1iwgQl3sGt18dMpXf8obemZVVg1ItXS3MkD5WJiTqe927RzELXxmBM2GUvoB3ThXGn7sytyjLNYCcDUh-2BDYX83p2T2yABh27he9EpHmqSqFR5qEJk5G8WNDjJVa3Nu2HeEH11NxDwiQkK7tLzeMpCwTe3bsQPO6dq-2Fo2-2F0zOo1tSjR-2FDNkwDMsNywweOSfDtyPJIq-2BhQh3fx0VtLXTWOXVlI4ReDl1M-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<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></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=GUvuDT95GpxW2sIYHiKg6obX3uIIobaj9awaKuut6Z93vqS7Y7ztKxl2CYIU1byQ6m4e2XZvWloRN4X0dWyHQt9fPrtkoUK4rSQhBk8y5nyIRoOzWl6ZOr4t228tIaHtgTc3VdWy3ckPlzwl61BHDY4p7HeqnNqIMN5lJKbtdjtjc0gTzQN8LbXMcTtPKDoVxnM1LBQnZhcaHwLGBh76rNGAgl9kwU1v8L9SoN7KwCM-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>