[swift-evolution] Proposal: Stored properties for enums

Tommy van der Vorst tommy at pixelspark.nl
Wed Dec 9 15:44:00 CST 2015


> None of the state-like nature of enums would be lost.
> 
> enum Something {
>  case StateA
>  case StateB(Double)
>  var prop: Int
> }
> 
> Would simply be a another way to write:
> 
> enum Something {
>  case StateA(Int)
>  case StateB(Double, Int)
> }


Sure, but do we really need special syntax then? To me the above way of writing is much clearer on which data is available at what point than the variant with the separate 'var' declaration. You can even label the different values in the associated data tuple.

Putting data shared across states in a separate 'var' declaration introduces some other issues as well: when an enum is reassigned (i.e. self = .StateB(...)), is the variable emptied somehow, or is it kept? How would you even initialize the value of a non-optional stored property that is not part of the case associated tuple (as the variables are not a 'requirement' of the case tuple, perhaps only optionals should be allowed)? 

/T



More information about the swift-evolution mailing list