I have the following code:
protocol MyEnum {
}
class MyClass {
func append(_ value: UInt8) {
print("uint8 \(value)")
}
func append(_ value: UInt16) {
print("uint16 \(value)")
}
func append<T: MyEnum>(_ value: T) {
print("myenum \(value)")
}
}
let c = MyClass()
func appendAny<T>(_ value: T) {
switch value {
case let u8 as UInt8:
c.append(u8)
case let u16 as UInt16:
c.append(u16)
case let myenum as MyEnum:
c.append(myenum)
default:
assert(false)
}
}
The error is reported by the compiler for the line c.append(myenum)
stating
Cannot invoke 'append' with an argument list of type (MyEnum)
Please, explain to me why this happens and what is the correct solution in this case. I want to preserve my overloads, since when called with a concrete MyEnum
instance, all works fine.