Imagine a protocol:
protocol Copyable
{
init(other: Self)
func copy() -> Self
}
Unfortunately, using this protocol is difficult because of the mention of Self in the init(:_) definition.
So, we change the parameter type:
protocol Copyable
{
init(other: Copyable)
func copy() -> Self
}
This now means that every implementing type has to cast the other
parameter to itself in order to access its members.
So, why not use a generic definition?
protocol Copyable
{
init<otherT : Copyable>(other: otherT)
func copy() -> Self
}
That's all well and good until it comes to implementing it, when the compiler insists that it needs to be implemented by the exact same generic signature.
What is more, it is still necessary to cast from otherT
to the implementing type, just as with using Copyable.
My "pitch" is that, in an implementing type should be able to satisfy the generic signature with a non-generic signature that matches all the requirements:
class Property<valueT : DefaultValueProvider & Equatable> : Copyable
{
var value = valueT()
init() { }
required convenience init(other: Property)
{
self.init()
value = other.value
}
}