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

Mateusz Malczak mateusz at malczak.info
Mon Oct 10 13:41:57 CDT 2016


> How about to a two type enums? By that I mean something like this:
>
> enum Enum : RawType, StoreType {
>     case a = (rawInstance1, storeInstance1)
>     case b = (rawInstance2, storeInstance2)
>     case c = rawInstance3, storeInstance3 // or not tuple like?
> }
>
> let instance = Enum.a
> instance.rawValue // would be rawInstance1
> instance.value // would be storeInstance1

I think its a bit too enigmatic, and stored properties should be
explicitly defined as part of enumeration type. Just like in case of
Java enums example.

enum Format: FormatStruct {
    let width: Double
    let height: Double

    case SMALL(width: 30, height: 30)
    case MEDIUM(width: 60, height: 60)
    case LARGE(width: 120, height: 120)

}

The way I see enumeration type with stored properties is something
different that enums with rawValue.

2016-10-10 20:33 GMT+02:00 Adrian Zubarev via swift-evolution
<swift-evolution at swift.org>:
> I haven’t followed the whole topic, but I myself always wished for stored
> properties on Swift enums.
>
> I’d like to throw an idea in the room.
>
> How about to a two type enums? By that I mean something like this:
>
> enum Enum : RawType, StoreType {
>     case a = (rawInstance1, storeInstance1)
>     case b = (rawInstance2, storeInstance2)
>     case c = rawInstance3, storeInstance3 // or not tuple like?
> }
>
> let instance = Enum.a
> instance.rawValue // would be rawInstance1
> instance.value // would be storeInstance1
>
> StoreType could be anything (even another two type enum).
>
> It would be interesting to read what you thing of that simple model. Plus
> should .value be mutable? Maybe we could communicate that somehow.
>
> Or we could simply allow tuples on enums where the first item is always the
> RawType of the enum?!
>
>
>
> --
> Adrian Zubarev
> Sent with Airmail
>
> Am 10. Oktober 2016 um 19:32:20, Kenny Leung via swift-evolution
> (swift-evolution at swift.org) schrieb:
>
> This is the way Java enumerations work.
>
> https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
>
> I think it is a good model, and I think Swift enumerations should also work
> the same way.
>
> An enumeration is a finite set of things. It’s really inconvenient to have
> to limit those things to have only a single attribute.
>
> -Kenny
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> 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
>


More information about the swift-evolution mailing list