<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="">Yes, that would be a great behaviour, but that’s exactly how enums with raw values do not work. The enum cases transform to and from an external representable form, that’s why it is ‘RawRepresentable’. So in your example of tuples, that representable form would be the tuple values themselves.</div><div class=""><br class=""></div><div class="">I agree about having enum cases as strings is handy. For your described case it is best to use a String as the raw value type, and the derived values as calculated properties. It would be handy, as we are discussing, to declare those calculated properties in an easier manner, and I would’t mind if it used a tuple syntax. But it should’t be used as the underlying value, as this is the source of ‘truth’ for the enum.</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 26 May 2016, at 12:09 PM, Leonardo Pessoa <<a href="mailto:me@lmpessoa.com" class="">me@lmpessoa.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div class=""><div class=""><div style="font-family: Calibri,sans-serif; font-size: 11pt;" class="">I'd still go with tuple syntax as it feels more like a natural extension to current enum syntax and does not introduce new elements to the syntax of enums (other than add tuples as a possible enum value type) thus being simpler and faster to implement and learn than a new syntax built specifically for this kind of construction.<br class=""><br class="">As I mentioned before, the issue with JSON and other engines trying to record the raw value instead of the enum seems to me as a wrong implementation choice of the engine. Previous to Swift enums I've always seen enum cases the same as constants and any additional values they'd hold are associated with that constant and not persisted. This may also be a thing from the company I work for today that choses to store the names of the enum cases (as strings) in databases and any values associated with them are recovered from the enum case constant. Of course the language I work with supports finding the enum value by its name, which it seems Swift doesn't.<br class=""><br class=""></div></div><div dir="ltr" class=""><hr class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">From: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class=""><a href="mailto:pgwsmith@gmail.com" class="">Patrick Smith</a></span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">Sent: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class="">25/05/2016 10:20 PM</span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">To: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class=""><a href="mailto:janis.kirsteins@gmail.com" class="">Jānis Kiršteins</a></span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">Cc: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class=""><a href="mailto:me@lmpessoa.com" class="">Leonardo Pessoa</a>; <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">Subject: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class="">Re: [swift-evolution] [Proposal] Enums with static stored propertiesforeach case</span><br class=""><br class=""></div>Yes, I don’t think it would work with a raw value behaviour. You want it to compile down to the same underlying code as the first example, without having to write lots of switch statements.<br class=""><br class="">Another syntax I could imagine is:<br class=""><br class="">enum Planet {<br class=""> var mass: Float { get }<br class=""> var radius: Float { get }<br class=""><br class=""> case mercury [<br class=""> mass: 3.303e+23,<br class=""> radius: 2.4397e6<br class=""> ]<br class=""> case venus [<br class=""> mass: 4.869e+24,<br class=""> radius: 6.0518e6<br class=""> ]<br class=""> case earth [<br class=""> mass: 5.976e+24,<br class=""> radius: 6.37814e6<br class=""> ]<br class=""> ...<br class="">}<br class=""><br class=""><br class="">You couldn’t have an initializer, as enums only have storage when they have associated values, which these do not. ‘where’ is used for pattern matching, not declaring as far as I know, so that’s why I suggest this other way.<br class=""><br class="">Patrick<br class=""><br class="">> On 26 May 2016, at 5:50 AM, Jānis Kiršteins via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">> <br class="">> That would replace current enum raw value functionality and I see two<br class="">> problems with that.<br class="">> <br class="">> 1. A lot of breaking changes<br class="">> 2. Raw values currently are unique values among all cases. That makes<br class="">> a possibility that enums can be easily serialized/deserialized to<br class="">> formats like JSON, property lists, etc. In "case mercury = (mass:<br class="">> 3.303e+23, radius: 2.4397e6)" neither mass nor radius is unique value<br class="">> (it is possible that two different planets could have the same mass as<br class="">> radius).<br class="">> <br class="">> <br class="">> <br class="">> On Wed, May 25, 2016 at 3:37 PM, Leonardo Pessoa <<a href="mailto:me@lmpessoa.com" class="">me@lmpessoa.com</a>> wrote:<br class="">>> Hi,<br class="">>> <br class="">>> Couldn't this be solved by using tuples? If not because the syntax is not<br class="">>> allowed I think this would be more coherent to do it using current syntax.<br class="">>> <br class="">>> enum Planet : (mass: Float, radius: Float) {<br class="">>> case mercury = (mass: 3.303e+23, radius: 2.4397e6)<br class="">>> case venus = (mass: 4.869e+24, radius: 6.0518e6)<br class="">>> case earth = (mass: 5.976e+24, radius: 6.37814e6)<br class="">>> case mars = (mass: 6.421e+23, radius: 3.3972e6)<br class="">>> case jupiter = (mass: 1.9e+27, radius: 7.1492e7)<br class="">>> case saturn = (mass: 5.688e+26, radius: 6.0268e7)<br class="">>> case uranus = (mass: 8.686e+25, radius: 2.5559e7)<br class="">>> case neptune = (mass: 1.024e+26, radius: 2.4746e7)<br class="">>> }<br class="">>> ________________________________<br class="">>> From: Jānis Kiršteins via swift-evolution<br class="">>> Sent: 25/05/2016 08:58 AM<br class="">>> To: <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">>> Subject: [swift-evolution] [Proposal] Enums with static stored properties<br class="">>> foreach case<br class="">>> <br class="">>> Hello everyone,<br class="">>> <br class="">>> Currently Swift only supports computed properties for each enum case.<br class="">>> If you want to somehow get static values with each case you would<br class="">>> probably do it like this:<br class="">>> <br class="">>> enum Planet {<br class="">>> case mercury<br class="">>> case venus<br class="">>> case earth<br class="">>> case mars<br class="">>> case jupiter<br class="">>> case saturn<br class="">>> case uranus<br class="">>> case neptune<br class="">>> <br class="">>> var mass: Float {<br class="">>> switch self {<br class="">>> case .mercury: return 3.303e+23<br class="">>> case .venus: return 4.869e+24<br class="">>> case .earth: return 5.976e+24<br class="">>> case .mars: return 6.421e+23<br class="">>> case .jupiter: return 1.9e+27<br class="">>> case .saturn: return 5.688e+26<br class="">>> case .uranus: return 8.686e+25<br class="">>> case .neptune: return 1.024e+26<br class="">>> }<br class="">>> }<br class="">>> <br class="">>> var radius: Float {<br class="">>> switch self {<br class="">>> case .mercury: return 2.4397e6<br class="">>> case .venus: return 6.0518e6<br class="">>> case .earth: return 6.37814e6<br class="">>> case .mars: return 3.3972e6<br class="">>> case .jupiter: return 7.1492e7<br class="">>> case .saturn: return 6.0268e7<br class="">>> case .uranus: return 2.5559e7<br class="">>> case .neptune: return 2.4746e7<br class="">>> }<br class="">>> }<br class="">>> }<br class="">>> <br class="">>> However I see two problems with this approach:<br class="">>> <br class="">>> 1. These value definitions are spread out and difficult to read and<br class="">>> maintain (especially if you have many computed properties for each<br class="">>> enum case);<br class="">>> 2. These values are not static. They are computed each time property<br class="">>> is accessed. This can be a problem when value is expensive to create.<br class="">>> <br class="">>> The proposed solution is to have single static initializer for each<br class="">>> enum case that initializes stored properties. For example,<br class="">>> <br class="">>> enum Planet {<br class="">>> var mass: Float<br class="">>> var radius: Float<br class="">>> <br class="">>> static init(mass: Float, radius: Float) {<br class="">>> self.mass = mass<br class="">>> self.radius = radius<br class="">>> }<br class="">>> <br class="">>> case mercury where (mass: 3.303e+23, radius: 2.4397e6)<br class="">>> case venus where (mass: 4.869e+24, radius: 6.0518e6)<br class="">>> case earth where (mass: 5.976e+24, radius: 6.37814e6)<br class="">>> case mars where (mass: 6.421e+23, radius: 3.3972e6)<br class="">>> case jupiter where (mass: 1.9e+27, radius: 7.1492e7)<br class="">>> case saturn where (mass: 5.688e+26, radius: 6.0268e7)<br class="">>> case uranus where (mass: 8.686e+25, radius: 2.5559e7)<br class="">>> case neptune where (mass: 1.024e+26, radius: 2.4746e7)<br class="">>> }<br class="">>> <br class="">>> This approach do not affect enums that have raw or associated values,<br class="">>> or custom enum initializers:<br class="">>> <br class="">>> case A = "A" where (id: 0)<br class="">>> <br class="">>> or<br class="">>> <br class="">>> case B(Int, Int, Int) where (id: 0)<br class="">>> <br class="">>> Benefits:<br class="">>> 1. Less verbosity<br class="">>> 2. Improved readability<br class="">>> 3. Related values are closer to each other<br class="">>> 4. Static values are not recomputed<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="">> _______________________________________________<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=""><br class=""></div></div></blockquote></div><br class=""></body></html>