<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=""><div class=""><div class="">So, just to recap, the proposed solution <i class="">is</i> to help enums expose associated values via properties, and is <i class="">not</i> to create enums that are open to extra unnamed cases (RectSize(width:0,height:10))? What I see is that enums would still maintain their standing where an instance is just a selection of a finite number of options, possibly with data attached. In proposal 1, we want some sort of syntax where this…</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: rgb(0, 143, 1);" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> RectSize</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> height:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(91, 38, 153);" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> width:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(91, 38, 153);" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> small(width: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">30</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, height: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">30</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> medium(width: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">60</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, height: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">60</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> large(width: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">120</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, height: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">120</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class="">…is syntactically just like writing this…</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: rgb(0, 143, 1);" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> RectSize</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> small</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> medium</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> large</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> height:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(91, 38, 153);" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 1);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">self</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .small: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">30</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .medium: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">60</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .large: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">90</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> width:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(91, 38, 153);" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 1);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">self</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .small: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">30</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .medium: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">60</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> .large: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 143, 1);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">90</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class="">…right? That way, you can write this:</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: rgb(0, 143, 1);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> size: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(170, 46, 184);" class="">RectSize</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = .small</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">size</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.height == </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">30</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(146, 146, 146);" class="">// true</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">size</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">.rawValue </span><span style="font-variant-ligatures: no-common-ligatures;" class="">// Error: RectSizes has no property `rawValue`.</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(146, 146, 146);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">size</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">.height = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(218, 32, 0);" class="">40</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">// Error: `height` is immutable</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">size</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = .medium</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class="">I think we were also (separately) proposing to extend `rawValue` to take all kinds of statically known values, like structs or tuples. Doing that would accomplish much of the same thing.</div><div class=""><br class=""></div><div class="">Someone fact-check me here! I really do think something like this would be a good idea, if we could get the right syntax.</div></div><br class=""><div style=""><blockquote type="cite" class=""><div class="">On Oct 11, 2016, at 7:06 AM, Mateusz Malczak 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=""><div class="">Hi,<br class="">I think we are here discussing two different aspects of introducing<br class="">this new feature - code syntax and underlying implementation.<br class="">In terms of code syntax I would go with first proposal as it seems to<br class="">me the simplest approach. When it comes to underlying implementation,<br class="">I can imagine that during compilation internal struct is created, as<br class="">well as any required property getters. This way you could get a<br class="">variation of rawValue implementation, at least from theoretical point<br class="">of view :D<br class=""><br class="">--<br class="">| Mateusz Malczak<br class="">+-------------------------------<br class="">| <a href="mailto:mateusz@malczak.info" class="">mateusz@malczak.info</a><br class="">| <a href="http://malczak.info" class="">http://malczak.info</a><br class=""><br class=""><br class="">2016-10-10 23:42 GMT+02:00 Haravikk <<a href="mailto:swift-evolution@haravikk.me" class="">swift-evolution@haravikk.me</a>>:<br class=""><blockquote type="cite" class=""><br class="">On 10 Oct 2016, at 20:34, Mateusz Malczak <<a href="mailto:mateusz@malczak.info" class="">mateusz@malczak.info</a>> wrote:<br class=""><br class="">I know, but what I'm saying is that this problem could be solved in the<br class="">multiple values case by allowing tuples as raw values for enums, since that<br class="">would allow you to specify both width and height. So it'd look something<br class="">like this:<br class=""><br class=""><br class="">We have three different possible solution<br class="">1. stored properties defined as part of enumeration type<br class="">enum RectSizes: MyRect<br class="">{<br class=""> let height:Int<br class=""> let width:Int<br class=""> case Small(width: 30, height: 30)<br class=""> case Medium(width: 60, height: 60)<br class=""> case Large(width: 120, height: 120)<br class="">}<br class=""><br class="">2. struct as rawValue<br class="">struct MyRect<br class="">{<br class=""> var height:Int<br class=""> var width:Int<br class=""> var area:Int {return height:Int*width}<br class="">}<br class=""><br class="">enum RectSizes: MyRect<br class="">{<br class=""> case Small(30,30)<br class=""> case Medium(60,60)<br class=""> case Large(120,120)<br class="">}<br class=""><br class="">3. tuples as rawValue<br class="">enum Format : (width:Int, height:Int) {<br class=""> case small(30, 30)<br class=""> case medium(60, 60)<br class=""> case large(120, 120)<br class=""><br class=""> var width:Int { return self.rawValue.width }<br class=""> var height:Int { return self.rawValue.height }<br class="">}<br class=""><br class="">Solutions 2 and 3 are quire similar, to get value of a stored property<br class="">we need to use rawValue or define value getters. In addition in<br class="">solution 2 we define an additional data type just to be used as an<br class="">enumeration type rawValue type. In my opinion, first approach would be<br class="">a best solution, type definition is clear and self-explanatory because<br class="">it is similar to how enums/classes are defined.<br class=""><br class=""><br class="">--<br class="">| Mateusz Malczak<br class=""><br class=""><br class="">Actually I'd say your option 2 here is more similar to option 1 (you seem to<br class="">be using a struct to define the stored properties instead). The issue here<br class="">is that storing properties conflicts with what you're actually doing, which<br class="">is storing case-specific values, which is what rawValue already does, it's<br class="">just too limited for your current use-case (multiple values).<br class=""><br class="">The complete solution would be to introduce the concept of tuples as<br class="">literals (even though they can't currently conform to types); this would<br class="">make it a lot easier to support the use of any type as a fixed value for<br class="">each case (not just tuples). For example, say we introduced as new protocol:<br class=""><br class="">protocol ExpressableByTuple {<br class="">associatedtype TupleType // somehow force this to be a tuple or<br class="">ExpressableByType type<br class="">init(tupleLiteral:TupleType)<br class="">}<br class=""><br class="">With a bit of magic all tuples could conform to this protocol with<br class="">themselves as the literal type, allowing us to use them as enum raw values;<br class="">likewise this could then be used to more easily enable any custom<br class="">struct/class for storage in enum cases, as instead of supporting their<br class="">constructors directly we can just support construction via a tuple literal.<br class=""><br class=""><br class="">My other reason I don't favour option 1, while it looks a bit prettier, is<br class="">that it's a bit confusing; enums have two types of stored properties, ones<br class="">that can be changed (and inspected) which is what you get when you declare<br class="">case small(Int, Int) for example, these are stored as part of the enum<br class="">itself (so in that example it's 17-bytes on a 64-bit system). However<br class="">rawValues are more like constants/static values, and don't increase the size<br class="">of the type, and I just feel that this is the right way to do what you're<br class="">proposing.<br class=""></blockquote>_______________________________________________<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></div></blockquote></div><br class=""></body></html>