On Sat, Jan 7, 2017 at 5:33 PM, Braeden Profile <[email protected]>
wrote:

>
>> Of course, I would love being able to use an initializer setup, but there
>> are serious bugs in the implementation.
>>
>> protocol Clonable
>> {
>>      init(other: Self)
>> }
>>
>> extension Clonable
>> {
>>      func clone() -> Self
>>              { return type(of: self).init(other: self) }
>> }
>>
>>
>> class Base: Clonable
>> {
>>      var x: Int
>>      
>>      init(x: Int)
>>              { self.x = x }
>>      
>>      required init(other: Base)
>>              { self.x = other.x }
>> }
>>
>> class Derived: Base
>> {
>>      var y: String
>>      
>>      init(x: Int, y: String)
>>      {
>>              self.y = y
>>              super.init(x: x)
>>      }
>>      
>>      // Should be required by the Clonable protocol, but it isn't.
>>      required init(other: Derived)
>>      {
>>              self.y = other.y
>>              super.init(other: other)
>>      }
>>      
>>      // Required because it was `required` in Base.  Even a `Derived` calls 
>> this initializer to clone, which is wrong.  Bugs abound.
>>      required init(other: Base)
>>              { fatalError("init(other:) is wrong.") }
>> }
>>
>>
>>
>> let me = Derived(x: 1, y: "food")
>> let alienClone = me.clone() // "init(other:) is wrong."
>>
>>
> Agree. That seems wrong. Great example.
>
>
> So, is this odd behavior intentional, a bug, or a design deficiency?  I
> would think that when a protocol has a method or initializer has `Self`
> parameters—like in Clonable—every subclass would be required to implement
> its own specialized version (much like a required initializer).  That would
> be a special case of the protocol system, though.
>
> As it sits, even fixing the calling behavior of my example leaves us with
> the problem of subclasses inheriting inapplicable required initializers
> from superclasses that actually don’t make any sense.
>
> Does this deserve its own thread?
>

Dunno, maybe best to have its own thread. It's not mentioned as part of
SE-0068, but IMO a complete design that respects the spirit of that
proposal *should* involve allowing you to write:

```
class Base : Clonable {
  required init(other: Self) { ... }
}

class Derived : Base {
  required init(other: Self) { ... }
}
```
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to