<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;">But that's an issue related to how serialising JSON works. Should the API serialise the name of the value and not the associated values, it should work fine. Should we have anything like annotations in Swift to describe how we wanted that output we'd have no problems.<br><br>About breaking things I wouldn't worry as Swift 3 is going to break a lot by itself anyway.<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:janis.kirsteins@gmail.com">Jānis Kiršteins</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 04:50 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:me@lmpessoa.com">Leonardo Pessoa</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: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>That would replace current enum raw value functionality and I see two<br>problems with that.<br><br>1. A lot of breaking changes<br>2. Raw values currently are unique values among all cases. That makes<br>a possibility that enums can be easily serialized/deserialized to<br>formats like JSON, property lists, etc. In "case mercury = (mass:<br>3.303e+23, radius: 2.4397e6)" neither mass nor radius is unique value<br>(it is possible that two different planets could have the same mass as<br>radius).<br><br><br><br>On Wed, May 25, 2016 at 3:37 PM, Leonardo Pessoa <me@lmpessoa.com> wrote:<br>> Hi,<br>><br>> Couldn't this be solved by using tuples? If not because the syntax is not<br>> allowed I think this would be more coherent to do it using current syntax.<br>><br>> enum Planet : (mass: Float, radius: Float) {<br>> case mercury = (mass: 3.303e+23, radius: 2.4397e6)<br>> case venus = (mass: 4.869e+24, radius: 6.0518e6)<br>> case earth = (mass: 5.976e+24, radius: 6.37814e6)<br>> case mars = (mass: 6.421e+23, radius: 3.3972e6)<br>> case jupiter = (mass: 1.9e+27, radius: 7.1492e7)<br>> case saturn = (mass: 5.688e+26, radius: 6.0268e7)<br>> case uranus = (mass: 8.686e+25, radius: 2.5559e7)<br>> case neptune = (mass: 1.024e+26, radius: 2.4746e7)<br>> }<br>> ________________________________<br>> From: Jānis Kiršteins via swift-evolution<br>> Sent: 25/05/2016 08:58 AM<br>> To: swift-evolution@swift.org<br>> Subject: [swift-evolution] [Proposal] Enums with static stored properties<br>> foreach case<br>><br>> Hello everyone,<br>><br>> Currently Swift only supports computed properties for each enum case.<br>> If you want to somehow get static values with each case you would<br>> probably do it like this:<br>><br>> enum Planet {<br>> case mercury<br>> case venus<br>> case earth<br>> case mars<br>> case jupiter<br>> case saturn<br>> case uranus<br>> case neptune<br>><br>> var mass: Float {<br>> switch self {<br>> case .mercury: return 3.303e+23<br>> case .venus: return 4.869e+24<br>> case .earth: return 5.976e+24<br>> case .mars: return 6.421e+23<br>> case .jupiter: return 1.9e+27<br>> case .saturn: return 5.688e+26<br>> case .uranus: return 8.686e+25<br>> case .neptune: return 1.024e+26<br>> }<br>> }<br>><br>> var radius: Float {<br>> switch self {<br>> case .mercury: return 2.4397e6<br>> case .venus: return 6.0518e6<br>> case .earth: return 6.37814e6<br>> case .mars: return 3.3972e6<br>> case .jupiter: return 7.1492e7<br>> case .saturn: return 6.0268e7<br>> case .uranus: return 2.5559e7<br>> case .neptune: return 2.4746e7<br>> }<br>> }<br>> }<br>><br>> However I see two problems with this approach:<br>><br>> 1. These value definitions are spread out and difficult to read and<br>> maintain (especially if you have many computed properties for each<br>> enum case);<br>> 2. These values are not static. They are computed each time property<br>> is accessed. This can be a problem when value is expensive to create.<br>><br>> The proposed solution is to have single static initializer for each<br>> enum case that initializes stored properties. For example,<br>><br>> enum Planet {<br>> var mass: Float<br>> var radius: Float<br>><br>> static init(mass: Float, radius: Float) {<br>> self.mass = mass<br>> self.radius = radius<br>> }<br>><br>> case mercury where (mass: 3.303e+23, radius: 2.4397e6)<br>> case venus where (mass: 4.869e+24, radius: 6.0518e6)<br>> case earth where (mass: 5.976e+24, radius: 6.37814e6)<br>> case mars where (mass: 6.421e+23, radius: 3.3972e6)<br>> case jupiter where (mass: 1.9e+27, radius: 7.1492e7)<br>> case saturn where (mass: 5.688e+26, radius: 6.0268e7)<br>> case uranus where (mass: 8.686e+25, radius: 2.5559e7)<br>> case neptune where (mass: 1.024e+26, radius: 2.4746e7)<br>> }<br>><br>> This approach do not affect enums that have raw or associated values,<br>> or custom enum initializers:<br>><br>> case A = "A" where (id: 0)<br>><br>> or<br>><br>> case B(Int, Int, Int) where (id: 0)<br>><br>> Benefits:<br>> 1. Less verbosity<br>> 2. Improved readability<br>> 3. Related values are closer to each other<br>> 4. Static values are not recomputed<br>> _______________________________________________<br>> swift-evolution mailing list<br>> swift-evolution@swift.org<br>> https://lists.swift.org/mailman/listinfo/swift-evolution<br></body></html>