[swift-evolution] [PROPOSAL]Return subclass type to a protocol where a superclass is defined without the need for associatedtype

Yogev Sitton yogev.sitton at gmail.com
Mon Apr 18 04:55:05 CDT 2016


That’s exactly my point - thank you for the quick summary Ross.
Protocols behaves differently than closures and classes.

> On Apr 18, 2016, at 12:40 PM, Ross O'Brien <narrativium+swift at gmail.com> wrote:
> 
> As of Swift 2.2, if a variable has a closure type of e.g. () -> Shape, a closure of type () -> Circle would be considered a match.  If a class implements 'func make() -> Shape', a subclass implementing 'func make() -> Circle' has to override. However, if a protocol requires a 'func make() -> Shape', a type implementing 'func make() -> Circle' isn't considered to be conforming. That does seem strange.
> 
> On Mon, Apr 18, 2016 at 12:55 AM, Yogev Sitton via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> In what way?
> 
> 
> 
> 
> 
> On Sun, Apr 17, 2016 at 3:53 PM -0700, "Howard Lovatt" <howard.lovatt at gmail.com <mailto:howard.lovatt at gmail.com>> wrote:
> 
> I think this should be part of completing generics.
> 
> On Sunday, 17 April 2016, Yogev Sitton via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> I have a class that is conforming to a protocol with a method that requires a specific return type.
> In case I want to return a subclass of the return type I am forced to use an associatedtype that feels like a hack.
> 
> As an example:
> 
> protocol MyShapeProtocol {
>     func make() -> Shape?
> }
> 
> class Circle : Shape {}
> 
> class CircleMaker : MyShapeProtocol{
>     func make() -> Circle? {
>         return Circle()
>     }
> }
> 
> This will not work.
> For that to work I’ll need to use toe associatedtype “hack”:
> 
> protocol MyShapeProtocol {
>     associatedtype ShapeReturnType : Shape
>     func make() -> ShapeReturnType?
> }
> 
> class Circle : Shape {}
> 
> class CircleMaker : MyShapeProtocol{
>     func make() -> Circle? {
>         return Circle()
>     }
> }
> 
> Is there a real value in adding the associatedtype line?
> 
> 
> -- 
> -- Howard.
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 

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


More information about the swift-evolution mailing list