[swift-evolution] [Proposal] Enums with static stored properties for each case
Jānis Kiršteins
janis.kirsteins at gmail.com
Sat May 28 08:54:38 CDT 2016
> - Abusing rawValue is just that: an abuse.
My original proposal does not replace rawValue and is compatible with it.
> - Using `where` just doesn't match the use of `where` elsewhere in the language; everywhere else, it's some kind of condition.
It is also used in generic type constraints. Plus it reads like human
language: `case mercury where (mass: 3.303e+23, radius: 2.4397e6)`
> - Dictionaries are the most straightforward way to handle this with the current language, but their lack of exhaustiveness checking is a problem.
Dictionaries can be used as workaround, but they cannot (lack of
exhaustiveness) solve the 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.
Wouldn't accessor just be a redundant keyword here? Currently enums do
not support stored properties, so I guess there is no extra need to
mark properties with any special keyword.
Property accessors might work for enums with associated values, but
not so well without them.
On Fri, May 27, 2016 at 3:43 PM, Brent Royal-Gordon via
swift-evolution <swift-evolution at swift.org> wrote:
>> The suggested solution based on 'accessor' - will create assotiated properties each time the enum instace created, for each instance of enum type.
>
> No; property accessors would be either computed or constant (so that all instances of a given case can share storage). This is much the way they would behave if they were included in behaviors.
>
> You could write a property accessor with a setter, but it would have to be computed, and manipulate `self`'s cases and associated values:
>
> enum Optional<T> {
> accessor var unwrapped: T { get set }
>
> case none {
> unwrapped {
> get { fatalError("No value") }
> set { self = .some(newValue) }
> }
> }
> case some (_ value: T) {
> unwrapped {
> get { return value }
> set { self = .some(newValue) }
> }
> }
> }
>
> --
> Brent Royal-Gordon
> Architechies
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list