Skip to content

Instantly share code, notes, and snippets.

@idrougge
Created November 11, 2019 15:10
Show Gist options
  • Save idrougge/5ad0063b3aa85d22bb5f850c5db9e21f to your computer and use it in GitHub Desktop.
Save idrougge/5ad0063b3aa85d22bb5f850c5db9e21f to your computer and use it in GitHub Desktop.
Protocol existential crisis
protocol P {}
struct A: P {}
struct B: P {}
struct C: P {}
var someP: P.Type = A.self
someP == A.self // true
someP == B.self // false
switch someP {
case A.self: print("It's an A") // Expression pattern of type 'A.Type' cannot match values of type 'P.Type'
default: print("It's some other P")
}
func ~=(rhs: P.Type, lhs: P.Type) -> Bool {
return rhs == lhs
}
switch someP {
case A.self: print("It's an A") // "It's an A"
default: print("It's some other P")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment