[swift-evolution] [Proposal] Enums with static stored properties for each case
Brent Royal-Gordon
brent at architechies.com
Thu May 26 16:47:31 CDT 2016
> The proposed solution is to have single static initializer for each
> enum case that initializes stored properties. For example,
My opinions so far:
- Abusing rawValue is just that: an abuse.
- Using `where` just doesn't match the use of `where` elsewhere in the language; everywhere else, it's some kind of condition.
- Dictionaries are the most straightforward way to handle this with the current language, but their lack of exhaustiveness checking is a problem.
What I would do is borrow the "accessors" concept from the property behaviors proposal and extend it so that it supported both functions and variables. Then I would let you write this:
enum Planet {
accessor var mass: Float
accessor var radius: Float
case mercury {
mass = 3.303e+23
radius = 2.4397e6
}
case venus {
mass = 4.869e+24
radius = 6.0518e6
}
case earth {
mass = 5.976e+24
radius = 6.37814e6
}
case mars {
mass = 6.421e+23
radius = 3.3972e6
}
case jupiter {
mass = 1.9e+27
radius = 7.1492e7
}
case saturn {
mass = 5.688e+26
radius = 6.0268e7
}
case uranus {
mass = 8.686e+25
radius = 2.5559e7
}
case neptune {
mass = 1.024e+26
radius = 2.4746e7
}
}
You would also be able to declare methods like this; each implementation would just look like `methodName { code }`. And you could provide default implementations too:
enum Planet {
accessor var mass: Float
accessor var radius: Float
accessor var habitable: Bool = false
case mercury {
mass = 3.303e+23
radius = 2.4397e6
}
case venus {
mass = 4.869e+24
radius = 6.0518e6
}
case earth {
mass = 5.976e+24
radius = 6.37814e6
habitable = true
}
case mars {
mass = 6.421e+23
radius = 3.3972e6
}
case jupiter {
mass = 1.9e+27
radius = 7.1492e7
}
case saturn {
mass = 5.688e+26
radius = 6.0268e7
}
case uranus {
mass = 8.686e+25
radius = 2.5559e7
}
case neptune {
mass = 1.024e+26
radius = 2.4746e7
}
}
--
Brent Royal-Gordon
Architechies
More information about the swift-evolution
mailing list