[swift-evolution] [Proposal] Enums with stored properties

J.E. Schotsman jeschot at xs4all.nl
Wed Oct 12 06:02:30 CDT 2016


> On 12 Oct 2016, at 09:41,Mateusz Malczak wrote:
> 
> That's exactly what this proposal is about. I would like to
> keep all enum properties but add an extra feature, so that enums can
> store some extra data.

If technically possible I would prefer to directly use types for enum cases.
Reducing a type to a tuple loses the computed properties and methods of the type.
The types must be equatable I suppose.

The current enum rawValues are just indexes or keys which the compiler can autogenerate (like it does for enums without a raw type). 
Of course the raw values can be used in some smart way, like for sorting or grouping cases, but this can all be done with general types - and better at that.

So basically you have a struct type and an enum which is just a set of static lets of the struct type.
struct MyStruct {}
enum MyEnum: MyStruct
	{
	case myStruct1
	case myStruct2
	...
	}

MyStruct need not be created specifically for the enum; it might be useful on its own.
MyEnum can have additional computed properties and methods which are needed for the enumerated values only.

The cases can be accessed like MyStruct instances. No need for .rawValue except in the case of primitive types. Even there there is room for sugar (if a variable is already known to be of type Int we can assign an enum:Int case to it with implied .rawValue).

Jan E.


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


More information about the swift-evolution mailing list