, , . enum CompareEnumMethod, , values type. compareEnum, . ==(lhs:,rhs:) , .value, compareEnumType .type. compareEnum.
enum CompareEnumMethod {
case type, value
}
protocol EquatableEnumType: Equatable {
static func compareEnumType(lhs: Self, rhs: Self) -> Bool
static func compareEnum(lhs: Self, rhs: Self, method: CompareEnumMethod) -> Bool
}
extension EquatableEnumType {
static func compareEnumType(lhs: Self, rhs: Self) -> Bool {
return Self.compareEnum(lhs: lhs, rhs: rhs, method: .type)
}
static func ==(lhs: Self, rhs: Self) -> Bool {
return Self.compareEnum(lhs: lhs, rhs: rhs, method: .value)
}
}
enum MyEnum: EquatableEnumType {
case A(Int)
case B
static func compareEnum(lhs: MyEnum, rhs: MyEnum, method: CompareEnumMethod) -> Bool {
switch (lhs, rhs, method) {
case let (.A(lhsA), .A(rhsA), .value):
return lhsA == rhsA
case (.A, .A, .type),
(.B, .B, _):
return true
default:
return false
}
}
}
let a0 = MyEnum.A(5)
let a1 = MyEnum.A(3)
let b0 = MyEnum.B
print(MyEnum.compareEnumType(lhs: a0, rhs: a1))
print(a0 == a1)
print(MyEnum.compareEnumType(lhs: a0, rhs: b0))