I am experimenting with protocols and generics. Stumbled upon this document:
Specifically this line
// error: protocol type 'Animal' cannot conform to 'Animal'...
This no longer applies, I can correctly compile and execute this code. Can someone please explain is the example no longer valid ? What exactly and when has it been introduced in the language to make this error disappear ?
1 Like
cukr
2
I believe it's because of Implicitly Opened Existentials
note that protocol types (aka existentials) still cannot conform to protocols (with the exception of Error)
2 Likes
bbrk24
3
Also, any protocols imported from Objective-C.
We should probably file an issue to revise this user doc to reflect the reduction in possible error surface now that implicitly-opened existentials are in.
3 Likes
Many thanks. Can you please guide me to understand how Error protocol conforms to itself ?
cukr
6
It's a special case hardcoded into the compiler. I cannot guide you, because I don't know it myself - I'm just using the compiler, not developing it.
If you want to figure it out yourself, here's a PR adding that
1 Like
Many thanks @cukr . From practical standpoint I understand this lets me write
func genericError<T: Error>(error: T) {
print(error.localizedDescription)
}
var error: Error
genericError(error: error)
Why was this added for Error specifically ? Now that we have implicit open existentials this will work for other protocol types as well - does that mean that the compiler feature specific for the Error protocol no longer applies ?
cukr
8
It was added to Error specifically to enable you to write Result<Foo, Error> mirroring a throwing function returning Foo
Existentials opening doesn't work here because you don't have anything to open in the success case :(
3 Likes