<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div><div style="font-family: Calibri,sans-serif; font-size: 11pt;">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><br>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><br></div></div><div dir="ltr"><hr><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">From: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;"><a href="mailto:pgwsmith@gmail.com">Patrick Smith</a></span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">Sent: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;">‎25/‎05/‎2016 10:20 PM</span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">To: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;"><a href="mailto:janis.kirsteins@gmail.com">Jānis Kiršteins</a></span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">Cc: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;"><a href="mailto:me@lmpessoa.com">Leonardo Pessoa</a>; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">Subject: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;">Re: [swift-evolution] [Proposal] Enums with static stored propertiesforeach case</span><br><br></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><br>Another syntax I could imagine is:<br><br>enum Planet {<br>&nbsp; var mass: Float { get }<br>&nbsp; var radius: Float { get }<br><br>&nbsp; case mercury [<br>&nbsp;&nbsp;&nbsp; mass: 3.303e+23,<br>&nbsp;&nbsp;&nbsp; radius: 2.4397e6<br>&nbsp; ]<br>&nbsp; case venus [<br>&nbsp;&nbsp;&nbsp; mass: 4.869e+24,<br>&nbsp;&nbsp;&nbsp; radius: 6.0518e6<br>&nbsp; ]<br>&nbsp; case earth [<br>&nbsp;&nbsp;&nbsp; mass: 5.976e+24,<br>&nbsp;&nbsp;&nbsp; radius: 6.37814e6<br>&nbsp; ]<br>&nbsp; ...<br>}<br><br><br>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><br>Patrick<br><br>&gt; On 26 May 2016, at 5:50 AM, Jānis Kiršteins via swift-evolution &lt;swift-evolution@swift.org&gt; wrote:<br>&gt; <br>&gt; That would replace current enum raw value functionality and I see two<br>&gt; problems with that.<br>&gt; <br>&gt; 1. A lot of breaking changes<br>&gt; 2. Raw values currently are unique values among all cases. That makes<br>&gt; a possibility that enums can be easily serialized/deserialized to<br>&gt; formats like JSON, property lists, etc. In "case mercury = (mass:<br>&gt; 3.303e+23, radius: 2.4397e6)" neither mass nor radius is unique value<br>&gt; (it is possible that two different planets could have the same mass as<br>&gt; radius).<br>&gt; <br>&gt; <br>&gt; <br>&gt; On Wed, May 25, 2016 at 3:37 PM, Leonardo Pessoa &lt;me@lmpessoa.com&gt; wrote:<br>&gt;&gt; Hi,<br>&gt;&gt; <br>&gt;&gt; Couldn't this be solved by using tuples? If not because the syntax is not<br>&gt;&gt; allowed I think this would be more coherent to do it using current syntax.<br>&gt;&gt; <br>&gt;&gt; enum Planet : (mass: Float, radius: Float) {<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case mercury = (mass: 3.303e+23, radius: 2.4397e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case venus = (mass: 4.869e+24, radius: 6.0518e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case earth = (mass: 5.976e+24, radius: 6.37814e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case mars = (mass: 6.421e+23, radius: 3.3972e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case jupiter = (mass: 1.9e+27, radius: 7.1492e7)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case saturn = (mass: 5.688e+26, radius: 6.0268e7)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case uranus = (mass: 8.686e+25, radius: 2.5559e7)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case neptune = (mass: 1.024e+26, radius: 2.4746e7)<br>&gt;&gt; }<br>&gt;&gt; ________________________________<br>&gt;&gt; From: Jānis Kiršteins via swift-evolution<br>&gt;&gt; Sent: ‎25/‎05/‎2016 08:58 AM<br>&gt;&gt; To: swift-evolution@swift.org<br>&gt;&gt; Subject: [swift-evolution] [Proposal] Enums with static stored properties<br>&gt;&gt; foreach case<br>&gt;&gt; <br>&gt;&gt; Hello everyone,<br>&gt;&gt; <br>&gt;&gt; Currently Swift only supports computed properties for each enum case.<br>&gt;&gt; If you want to somehow get static values with each case you would<br>&gt;&gt; probably do it like this:<br>&gt;&gt; <br>&gt;&gt; enum Planet {<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case mercury<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case venus<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case earth<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case mars<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case jupiter<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case saturn<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case uranus<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case neptune<br>&gt;&gt; <br>&gt;&gt;&nbsp;&nbsp;&nbsp; var mass: Float {<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch self {<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .mercury: return 3.303e+23<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .venus: return 4.869e+24<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .earth: return 5.976e+24<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .mars: return 6.421e+23<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .jupiter: return 1.9e+27<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .saturn: return 5.688e+26<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .uranus: return 8.686e+25<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .neptune: return 1.024e+26<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&gt;&gt;&nbsp;&nbsp;&nbsp; }<br>&gt;&gt; <br>&gt;&gt;&nbsp;&nbsp;&nbsp; var radius: Float {<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch self {<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .mercury: return 2.4397e6<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .venus: return 6.0518e6<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .earth: return 6.37814e6<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .mars: return 3.3972e6<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .jupiter: return 7.1492e7<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .saturn: return 6.0268e7<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .uranus: return 2.5559e7<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case .neptune: return 2.4746e7<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&gt;&gt;&nbsp;&nbsp;&nbsp; }<br>&gt;&gt; }<br>&gt;&gt; <br>&gt;&gt; However I see two problems with this approach:<br>&gt;&gt; <br>&gt;&gt; 1. These value definitions are spread out and difficult to read and<br>&gt;&gt; maintain (especially if you have many computed properties for each<br>&gt;&gt; enum case);<br>&gt;&gt; 2. These values are not static. They are computed each time property<br>&gt;&gt; is accessed. This can be a problem when value is expensive to create.<br>&gt;&gt; <br>&gt;&gt; The proposed solution is to have single static initializer for each<br>&gt;&gt; enum case that initializes stored properties. For example,<br>&gt;&gt; <br>&gt;&gt; enum Planet {<br>&gt;&gt;&nbsp;&nbsp;&nbsp; var mass: Float<br>&gt;&gt;&nbsp;&nbsp;&nbsp; var radius: Float<br>&gt;&gt; <br>&gt;&gt;&nbsp;&nbsp;&nbsp; static init(mass: Float, radius: Float) {<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.mass = mass<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.radius = radius<br>&gt;&gt;&nbsp;&nbsp;&nbsp; }<br>&gt;&gt; <br>&gt;&gt;&nbsp;&nbsp;&nbsp; case mercury where (mass: 3.303e+23, radius: 2.4397e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case venus where (mass: 4.869e+24, radius: 6.0518e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case earth where (mass: 5.976e+24, radius: 6.37814e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case mars where (mass: 6.421e+23, radius: 3.3972e6)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case jupiter where (mass: 1.9e+27, radius: 7.1492e7)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case saturn where (mass: 5.688e+26, radius: 6.0268e7)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case uranus where (mass: 8.686e+25, radius: 2.5559e7)<br>&gt;&gt;&nbsp;&nbsp;&nbsp; case neptune where (mass: 1.024e+26, radius: 2.4746e7)<br>&gt;&gt; }<br>&gt;&gt; <br>&gt;&gt; This approach do not affect enums that have raw or associated values,<br>&gt;&gt; or custom enum initializers:<br>&gt;&gt; <br>&gt;&gt; case A = "A" where (id: 0)<br>&gt;&gt; <br>&gt;&gt; or<br>&gt;&gt; <br>&gt;&gt; case B(Int, Int, Int) where (id: 0)<br>&gt;&gt; <br>&gt;&gt; Benefits:<br>&gt;&gt; 1. Less verbosity<br>&gt;&gt; 2. Improved readability<br>&gt;&gt; 3. Related values are closer to each other<br>&gt;&gt; 4. Static values are not recomputed<br>&gt;&gt; _______________________________________________<br>&gt;&gt; swift-evolution mailing list<br>&gt;&gt; swift-evolution@swift.org<br>&gt;&gt; https://lists.swift.org/mailman/listinfo/swift-evolution<br>&gt; _______________________________________________<br>&gt; swift-evolution mailing list<br>&gt; swift-evolution@swift.org<br>&gt; https://lists.swift.org/mailman/listinfo/swift-evolution<br><br></body></html>