<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">There are legit cases where you want to be able to subclass the class within a module (framework), but you don't want others to subclass it out of the framework in order to provide some integrity. This is usually done nowadays (on many places within Apple's frameworks) by an assertion in init.</div><div class=""><br class=""></div><div class="">It is very similar to declaring public internal(set) var - but with a class and subclassing instead of modifying the contents of a var.</div><div class=""><br class=""></div><div class="">And without it, you can't do this efficiently - the default case is dangerous if you have a variable that returns non-nil value depending on the class - the default case usually has fatalError() inside - forgetting to add the clause for a new class won't be discovered until runtime.</div><div class=""><br class=""></div><div class="">I personally bypass this currently by having an enum of classes defined with an initializer init(instance:) and then switch by the enum, which is safer than switch by type - but it's extra work.</div><div class=""><br class=""></div><div class="">Charlie</div><div class=""><br class="">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On May 25, 2016, at 4:41 AM, Leonardo Pessoa via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><div style="font-family: Calibri,sans-serif; font-size: 11pt;" class="">Limiting the amount of subclasses is not really a good idea as you would need to introduce another mechanism in the language while the proposed feature requires much less. And you're thinking only about the restrictive set (internal and private) and forgetting the more open end (public). Why is it so bad for this proposal to support requiring the default case? If its possible for the compiler to discover you covered all possible cases it would be fine not having default but IMHO in most cases it will find out there are more not explicitly covered.<br class=""></div></div><div dir="ltr" class=""><hr class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">From: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class=""><a href="mailto:davesweeris@mac.com" class="">David Sweeris</a></span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">Sent: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class="">24/05/2016 11:01 PM</span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">To: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class=""><a href="mailto:austinzheng@gmail.com" class="">Austin Zheng</a></span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">Cc: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class=""><a href="mailto:me@lmpessoa.com" class="">Leonardo Pessoa</a>; <a href="mailto:swift-evolution@swift.org" class="">swift-evolution</a></span><br class=""><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;" class="">Subject: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;" class="">Re: [swift-evolution] [Pitch] Exhaustive pattern matching forprotocols and classes</span><br class=""><br class=""></div><div class="">Or if there was a way to declare that a class/protocol can only have a defined set of subclasses/conforming types.<br class=""><br class="">Sent from my iPhone</div><div class=""><br class="">On May 24, 2016, at 15:35, Austin Zheng via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">If you pattern match on a type that is declared internal or private, it is impossible for the compiler to not have an exhaustive list of subclasses that it can check against.<div class=""><br class=""></div><div class="">Austin</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, May 24, 2016 at 1:29 PM, Leonardo Pessoa <span dir="ltr" class=""><<a href="mailto:me@lmpessoa.com" target="_blank" class="">me@lmpessoa.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid;">I like this but I think it would be a lot hard to ensure you have all<br class="">
subclasses covered. Think of frameworks that could provide many<br class="">
unsealed classes. You could also have an object that would have to<br class="">
handle a large subtree (NSObject?) and the order in which the cases<br class="">
are evaluated would matter just as in exception handling in languages<br class="">
such as Java (or require some evaluation from the compiler to raise<br class="">
warnings). I'm +1 for this but these should be open-ended like strings<br class="">
and require the default case.<br class="">
<br class="">
On 24 May 2016 at 17:08, Austin Zheng via swift-evolution<br class="">
<div class="HOEnZb"><div class="h5"><<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">
> I have been hoping for the exhaustive pattern matching feature for a while<br class="">
> now, and would love to see a proposal.<br class="">
><br class="">
> Austin<br class="">
><br class="">
> On Tue, May 24, 2016 at 1:01 PM, Matthew Johnson via swift-evolution<br class="">
> <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">
>><br class="">
>> Swift currently requires a default pattern matching clause when you switch<br class="">
>> on an existential or a non-final class even if the protocol or class is<br class="">
>> non-public and all cases are covered. It would be really nice if the<br class="">
>> default clause were not necessary in this case. The compiler has the<br class="">
>> necessary information to prove exhaustiveness.<br class="">
>><br class="">
>> Related to this is the idea of introducing something like a `sealed`<br class="">
>> modifier that could be applied to public protocols and classes. The<br class="">
>> protocol or class would be visible when the module is imported, but<br class="">
>> conformances or subclasses outside the declaring module would be prohibited.<br class="">
>> Internal and private protocols and classes would implicitly be sealed since<br class="">
>> they are not visible outside the module. Any protocols that inherit from a<br class="">
>> sealed protocol or classes that inherit from a sealed class would also be<br class="">
>> implicitly sealed (if we didn’t do this the sealing of the superprotocol /<br class="">
>> superclass could be violated by conforming to or inheriting from a<br class="">
>> subprotocol / subclass).<br class="">
>><br class="">
>> Here are examples that I would like to see be valid:<br class="">
>><br class="">
>> protocol P {}<br class="">
>> // alternatively public sealed protocol P {}<br class="">
>> struct P1: P {}<br class="">
>> struct P2: P {}<br class="">
>><br class="">
>> func p(p: P) -> Int {<br class="">
>> switch p {<br class="">
>> case is P1: return 1 // alternatively an `as` cast<br class="">
>> case is P2: return 2 // alternatively an `as` cast<br class="">
>> }<br class="">
>> }<br class="">
>><br class="">
>> class C {}<br class="">
>> // alternatively public sealed class C {}<br class="">
>> class C1: C {}<br class="">
>> class C2: C {}<br class="">
>><br class="">
>> func c(c: C) -> Int {<br class="">
>> switch c {<br class="">
>> case is C1: return 1 // alternatively an `as` cast<br class="">
>> case is C2: return 2 // alternatively an `as` cast<br class="">
>> case is C: return 0 // alternatively an `as` cast<br class="">
>> }<br class="">
>> }<br class="">
>><br class="">
>> I am wondering if this is something the community is interested in. If<br class="">
>> so, I am wondering if this is something that might be possible in the Swift<br class="">
>> 3 timeframe (maybe just for private and internal protocols and classes) or<br class="">
>> if it should wait for Swift 4 (this is likely the case).<br class="">
>><br class="">
>> -Matthew<br class="">
>> _______________________________________________<br class="">
>> swift-evolution mailing list<br class="">
>> <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" rel="noreferrer" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
><br class="">
><br class="">
><br class="">
> _______________________________________________<br class="">
> swift-evolution mailing list<br class="">
> <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" rel="noreferrer" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
><br class="">
</div></div></blockquote></div><br class=""></div>
</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>