[swift-evolution] Fixing raw enum types

David Waite david at alkaline-solutions.com
Mon Jan 16 15:02:05 CST 2017


Or perhaps paint the bikeshed 

enum Something<(Int32, Int32)> { … }

-DW
> On Jan 16, 2017, at 11:51 AM, Anton Zhilin via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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 <mailto: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
> }
> _______________________________________________
> swift-evolution mailing list
> 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/20170116/f31cb756/attachment.html>


More information about the swift-evolution mailing list