[swift-evolution] Proposal: Stored properties for enums
Jonathan Hise Kaldma
info at hisekaldma.com
Wed Dec 9 15:11:15 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)
}
The properties would just be like associated values that are included for every case. Of course when there's only two cases and one property, it's not a problem. But if you have more cases and more common values, it quickly gets out of hand.
I don't really see how the containing struct solves the problem in the example, since Expression only has other Expressions as children. You could of course modify it to contain Captures instead, but that doesn't express the recursiveness of expressions in the same elegant way.
Best,
Jonathan
> 9 dec. 2015 kl. 21:36 skrev Tommy van der Vorst <tommy at pixelspark.nl>:
>
> Hi Jonathan,
>
> One of the great things about enums with attached values is that they define discrete 'states' in which the enum can be, and what values to expect in each state. If you want to add information to an enum value that is not related to its state, it sounds to me like you rather should use a struct for that (possibly embedding the 'state' part), like you say, i.e.:
>
> enum Expression {
> case Number(Double)
> case Variable(String)
> indirect case Unary(Operator, Expression)
> indirect case Binary(Operator, Expression, Expression)
> }
>
> struct Capture {
> let location: Int
> let length: Int
> let expression: Expression
>
> init(_ expression: Expression, location: Int, length: Int) {...}
> }
>
> let expr = Capture(.Number(3.0), location: .., length: ...)
>
> In my view, adding independent stored properties to enum would make this not much easier to write or read, but only more confusion for people expecting the 'state machine' like behaviour of enum.
>
> Best,
> Tommy.
>
>
>> Op 9 dec. 2015, om 21:28 heeft Jonathan Hise Kaldma via swift-evolution <swift-evolution at swift.org> het volgende geschreven:
>>
>> Hi everyone,
>>
>> Being able to associate values with enums is one of the features I love about Swift, especially together with the ability to make enums contain themselves. It makes it really easy to model tree-like structures, e.g. arithmetic expressions:
>>
>> enum Expression {
>> case Number(Double)
>> case Variable(String)
>> indirect case Unary(Operator, Expression)
>> indirect case Binary(Operator, Expression, Expression)
>> }
>>
>> This works great with Swift’s switch statement and let binding, and cuts out a lot of unnecessary code. But it breaks down if you need to keep track of more data for each tree node, e.g. if you’re making a parser and need to keep track of source location. This could easily be solved with stored properties:
>>
>> enum Expression {
>> case Number(Double)
>> case Variable(String)
>> indirect case Unary(Operator, Expression)
>> indirect case Binary(Operator, Expression, Expression)
>>
>> var location: Int = 0
>> var length: Int = 0
>> }
>>
>> If the properties have default values, you would still get the regular enum initializer without properties like today:
>>
>> let expr = .Number(3)
>>
>> But you would also get a memberwise initializer with the properties after the associated values, which you can use if you the properties don’t have default values or if you just want to set them to something else.
>>
>> let expr = .Number(3, location: 5, length: 1)
>>
>> Other than that, enums would work just like they do today. Aside from the previous example, I think this would simplify a lot of use cases where you have a struct and an accompanying enum, so rather than:
>>
>> struct Something {
>> var type: SomethingType
>> var width: Int
>> var height: Int
>> }
>>
>> enum SomethingType {
>> case TypeA
>> case TypeB
>> }
>>
>> You would just have:
>>
>> enum Something {
>> case TypeA
>> case TypeB
>>
>> var width: Int
>> var length: Int
>> }
>>
>> Feels very Swifty to me.
>>
>> Thanks,
>> Jonathan
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151209/d6238038/attachment-0001.html>
More information about the swift-evolution
mailing list