[swift-evolution] [Pitch] consistent public access modifiers

Adrian Zubarev adrian.zubarev at devandartist.com
Sun Feb 12 03:53:52 CST 2017


Hi Brent, I understand that SubC.cc as C does not work, that’s the case in my bikeshedding example I noticed that because the enum case is always known at compile time, there will be an error with informs you to downgrade SubC to one of its super-types case. Plus I forget about the RawRepresentable for enums while making this example completely - my bad.

I might not fully understand how sub-typing on value types should work, that’s way I’m looking forward to the document Matthew will write one day. :)

Sub-typing enums to remove cases sounds also really interesting to me. :)

Thank you for your feedback.



-- 
Adrian Zubarev
Sent with Airmail

Am 12. Februar 2017 um 09:57:39, Brent Royal-Gordon (brent at architechies.com) schrieb:

> On Feb 11, 2017, at 2:25 AM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:  
>  
> // Allowed because `C` is open, and open allows sub-typing, conforming  
> // and overriding to the client  
> enum SubC : C {  
> case cc  
> }  

There's a subtle mistake here. For a sum type, subtyping actually means *removing* cases, not *adding* cases. (Or it can mean changing some of the associated types into their subtypes, but that's a different story.)  

To understand why, think about casting between `C` and `SubC`. Specifically, imagine this upcast:  

SubC.cc as C  

What is this supposed to do? There is no `cc` case in `C`, so `SubC.cc` cannot be represented as a `C`. That means `SubC` is not a subtype of `C`. In fact, `SubC` would be a *supertype* of `C`—you would need to say `SubC.cc as? C`, but you could always say `C.c as SubC`.  

(The ultimate expression of this is that `Never`—which can be thought of as the subtype of all types—is an empty enum.)  

That's not to say that this is a feature we should add; rather, it's to say that the concept of inheritance doesn't really make any sense for `enum`s.  

Just something I noticed when I was reading this thread. I'll have more to say on the main topic, hopefully soon.  

--  
Brent Royal-Gordon  
Architechies  

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170212/0ff27330/attachment.html>


More information about the swift-evolution mailing list