[swift-evolution] Proposal: Stored properties for enums

Dave DeLong delong at apple.com
Wed Dec 9 15:06:32 CST 2015


I came across the exact same problem a couple of months ago (expressions with source ranges).

The issue with the struct-and-enum approach is that it increases the amount of indirection you have in order to get at the data you care about. When dealing with expressions, you almost always care about the expression type (i.e., the enum value). Very rarely do you need the range of the original expression in the source string. However, the struct-and-enum approach optimizes for the accessing range, but not the type and its associated values.

I would be very much in favor of seeing stored properties on enums.

Dave

> On Dec 9, 2015, at 1:36 PM, Tommy van der Vorst via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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 <mailto: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 <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> _______________________________________________
> 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/379c76ce/attachment.html>


More information about the swift-evolution mailing list