<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;">Hi,<br><br>Couldn't this be solved by using tuples? If not because the syntax is not 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></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:swift-evolution@swift.org">Jānis Kiršteins via swift-evolution</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 08:58 AM</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: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;">[swift-evolution] [Proposal] Enums with static stored properties foreach case</span><br><br></div>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>