Why Do Enums Have Computed Properties But Not Stored Properties in Swift

Why do enums have computed properties but not stored properties in Swift?

enums do have stored type properties - i.e., static properties. They don't have stored instance properties. I don't know if there is a technical reason why stored instance properties are not available for enums. You may have to ask your question on the dev forum if you want a technical answer for "why".

In your question you ask if associated values work like stored properties. In fact, they do, and are more flexible (in some ways) than stored properties for structs and classes. Each case in an enum can have its own specialized set of data that is associated with it. Rather than have one set of stored properties that apply to all cases, you get to individualize the stored properties for each case.

Enum conforming to protocol with stored property

OK, so the error you are getting is because all properties must be computed. So, in your simple example, you could do something like this:

enum SimpleEnum: ExampleProtocol {
case foo, bar

var simpleDescription: String {
switch self {
case .foo: return "Foo"
case .bar: return "Bar"
}
}

func adjust() {

}

}

Creating enums with specific values/stored properties

Swift does not support stored properties in enums

You could make xAlter and yAlter computed properties. Here's a highly simplified example (only two cardinal directions):

enum CompassDirection {
case n
case s
var alterX : Int {
switch self {
case .n: return 0
case .s: return 0
}
}
var alterY : Int {
switch self {
case .n: return 1
case .s: return -1
}
}
}

Now you can add a method referring to self.alterX and self.alterY, exactly as you desire.

Static properties in enum

Why shouldn't you be able to do this?

Swift enums are first-class types, just like structs and classes. Swift enums do not need to have cases, they can be completely empty types, just like how a struct or class does not need to have any properties.

enum Empty {} // completely valid

enums cannot have _ stored instance properties_, but they can have type properties (which static properties are) and computed instance properties.

Caseless enums with static properties are often used for storing constant values. For more information on the topic, see Swift constants struct or enum

This might not be documented in the Enumerations section of the Swift docs, but nothing says that this shouldn't be possible either. On the other hand, the docs do state that enums are first-class types and there is a non-exhaustive list of features that enums share with classes and structs.

Why Swift Enum generate object, internal variable can't be altered?

You have made your MyEnum var some a computed property. A computed property gets evaluated every time you read its value. Thus, every time you invoke x?.some, you get a new Some object.

As Rob pointed out, my initial suggestion for a way to fix this won't work. Enums can't have stored properites. I think you'll have to use an associated value on your enum case to do what you want.

Why no stored type properties for classes in swift?

The compiler error is "Class variables not yet supported" so it seems like they just haven't implemented it yet.



Related Topics



Leave a reply



Submit