[swift-evolution] Allow protocol vars to match derived types

Mark Anders mark at markanders.net
Mon Mar 7 17:14:31 CST 2016


So it seems like you would expect it to work as I thought it should. However, from reading the language reference 
there is no mention of whether derived types are conforming types and there are no examples shown that demonstrate this either.

Can someone who knows verify whether this is a bug, or an oversight or as designed?

The workaround for me is to move everything in the base class which neuters the type system somewhat.

Mark

On March 7, 2016 at 3:53:37 PM, Howard Lovatt (howard.lovatt at gmail.com) wrote:

Looks like a compiler bug to me. 

On Tuesday, 8 March 2016, Mark Anders via swift-evolution <swift-evolution at swift.org> wrote:
Consider the following (you can paste it in a Playground to see the error):

class Node { }
class Containable : Node{}


protocol Refers {
    var to : Node {get}
}


class Link : Refers {
    var to : Node
    init(n : Node) {
        to = n
    }
}


class Contains : Refers {
    var to : Containable
    init(c : Containable) {
        to = c
    }
}

This currently does not work because it seems that to adopt a protocol, the type of protocol var must match exactly.

It would be great if objects could be said to adopt a protocol if the type of the var is the type or a derived type.  
This would allow me to treat the structure in a type safe way (i.e. only a Containable can have a Contains relationship), 
while me to have a set of Refers and iterate through each Node.

Is there a reason why the type must match exactly?  Or could protocols be enhanced to to allow matching
derived types, similar to assignment and func parameter rules?  


Mark


--
-- Howard.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160307/9d51067f/attachment.html>


More information about the swift-evolution mailing list