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

Brent Royal-Gordon brent at architechies.com
Sun Feb 12 02:57:35 CST 2017


> 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



More information about the swift-evolution mailing list