Created
November 11, 2019 15:10
-
-
Save idrougge/5ad0063b3aa85d22bb5f850c5db9e21f to your computer and use it in GitHub Desktop.
Protocol existential crisis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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