<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi all,</div><div class=""><br class=""></div>here is the scenario, I want to implement a Copying protocol which can be used homogeneously<div class=""><br class=""></div><div class=""><span class="" style="font-family: Menlo;">protocol Copying {</span><br class="" style="font-family: Menlo;"><span class="" style="font-family: Menlo;"> func copy() -> Self</span><br class="" style="font-family: Menlo;"><span class="" style="font-family: Menlo;">}</span></div><div class=""><span class="" style="font-family: Menlo;"><br class=""></span></div><div class="">Because this protocol contains neither associated-type-requirements or self-requirements(requiring a method returning Self is different), it can surely be used homogeneously.</div><div class=""><br class=""></div><div class=""><div class=""><span class="" style="font-family: Menlo;">let objects: [Copying] = ....</span></div></div><div class=""><span class="" style="font-family: Menlo;">for copyable in objects {</span></div><div class=""><span class="" style="font-family: Menlo;"> .....</span></div><div class=""><span class="" style="font-family: Menlo;">}</span></div><div class=""><br class=""></div><div class="">It will work if I make one of my base class conform to Copying</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">class Shape: Copying {</font></div><div class=""><span class="" style="font-family: Menlo;"> var color: UIColor?</span></div><div class=""><span class="" style="font-family: Menlo;"><br class=""></span></div><div class=""><font face="Menlo" class=""> required init() {}</font></div><div class=""><font face="Menlo" class=""> </font></div><div class=""><font face="Menlo" class=""> func copy() -> Self {</font></div><div class=""><font face="Menlo" class=""> let copied = type(of: self).init()</font></div><div class=""><font face="Menlo" class=""> copied.color = color</font></div><div class=""><font face="Menlo" class=""> return copied</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">The implementation of `copy` above is forced by compiler to avoid any explicit specification of type `Shape`, so that the returning value will have a dynamic type, which is just what I want. Here, the type of `copied` is `Self`</div><div class=""><br class=""></div><div class="">However, if I try to make a subclass of Shape, I can't find a elegant way to implement this `copy` method in that subclass, the following code will not compile.</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">class Circle: Shape {</font></div><div class=""><font face="Menlo" class=""> var radius: Float = 5</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""> func copy() -> Self {</font></div><div class=""><font face="Menlo" class=""> let copied = super.copy() </font></div><div class=""><font face="Menlo" class=""> copied.radius = radius </font><span class="" style="font-family: Menlo;"> // compilation error</span></div><div class=""><font face="Menlo" class=""> return copied</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">The compiler will complain that `copied` has no property `radius`. It turns out that calling copy() on super will yield a value of type Shape, rather than Self. </div><div class=""><br class=""></div><div class="">Swift now forbids explicit conversion to `Self` (I totally agree with this rule), and will automatically allow `Self` to be treated as a specific type in some circumstances. But for this case, I wonder whether this is the correct behavior or a bug? Why calling `super.copy()` not be able to get a `Self`?</div><div class=""><br class=""></div><div class="">I did find a work-around for this problem afterwards, but this question really haunts me...</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Lincoln.</div></body></html>