[swift-evolution] Proposal: Stored properties for enums

Tommy van der Vorst tommy at pixelspark.nl
Wed Dec 9 14:36:19 CST 2015


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151209/a7d700cf/attachment.html>


More information about the swift-evolution mailing list