Swift uses various overloads of the ~=
operator to do pattern matching, which also lets us define our own overloads ... ๐ Sundell
๐พ ็จๅผ๏ผpaiza.io
โฌ๏ธ ้่ฆ๏ผCaseReflectable
// ------------
// Enum
// ------------
// enum with associated values
// (conforms to `CaseReflectable`)
enum Enum: CaseReflectable {
case int(Int)
case int2(Int)
case person(name: String, age: Int)
case str(String)
}
// ------------
// main
// ------------
let a: Enum = .int(3)
Enum.int ~= a // true
Enum.int2 ~= a // false
let joe = Enum.person(name: "joe", age: 8)
Enum.person ~= joe // true
Enum.int ~= joe // false