[swift-evolution] Fixing raw enum types

Anton Zhilin antonyzhilin at gmail.com
Mon Jan 16 12:51:23 CST 2017


This idea by Karl made me branch off a new thread.

2017-01-16 21:28 GMT+03:00 Karl Wagner <razielim at gmail.com>:

It would be helpful for synthesised RawRep conformance. The
> inheritance-like syntax we have now is awful - it makes people think that
> RawRepresentable is some kind of magic protocol that will allow special
> compiler jango to happen.
>
> You could see why they think that. This looks very much like the enum is
> going to *be* an Int32:
>
> enum Something: Int32 {
>     case oneThing = 36
>     case anotherThing = 42
> }
>
> This is also one of the icky parts to allowing tuples of integer/string
> literals (something people ask for quite a lot). It would look like you’re
> deriving your enum from a non-nominal type:
>
> enum Something: (Int32, Int32) {
>     case oneThing = (3, 12)
>     case anotherThing = (5, 9)
> }
>
> Even if Swift gains newtype, it’s not that case. The enum does not gain
methods or behavior of the specified type. It’s just a shorthand, hinting
the compiler to provide correct RawRepresentable conformance.

I suggest to invent a new syntax for this type hinting for RawRepresentable.
For example, it can be an annotation:

@raw(Int32) enum Something {
    // ...
}

Or a contextual keyword:

enum Something : raw(Int32) {
    // ...
}

Perhaps, he most uniform and explicit of syntaxes:

enum Something : RawRepresentable {
    case oneThing = 36
    case anotherThing = 42
}

enum AnotherThing : RawRepresentable {
    typealias RawValue = Int32
    case oneThing
    case anotherThing
}

​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170116/95abfa59/attachment.html>


More information about the swift-evolution mailing list