How to override switch case behaviour like we override operator == with Equatable protocol

Equatable protocol not called when enum compared in switch case.

Here is my code

`import Foundation

public enum AMSelectionStyle{

    case single

    case multiple

    case any

}

public enum AMCalenderMode{

    case date

    case time

    case dateTime

    case any

    

}

public enum AMNumberType:String{

    case natural = "N" //(also called positive integers, counting numbers, or natural numbers); They are the numbers {1, 2, 3, 4, 5, …}

    case whole = "W" //This is the set of  natural numbers, plus zero, i.e., {0, 1, 2, 3, 4, 5, …}.

    case integer = "Z" // This is the set of all whole numbers plus all the negatives (or opposites) of the natural numbers, i.e., {… , ⁻2, ⁻1, 0, 1, 2, …}

    case rational = "Q" //This is all the fractions where the top and bottom numbers are integers; e.g., 1/2, 3/4, 7/2, ⁻4/3, 4/1 [Note: The denominator cannot be 0, but the numerator can be].

    case real = "R" //(also called measuring numbers or measurement numbers). This includes all numbers that can be written as a decimal. This includes fractions written in decimal form e.g., 0.5, 0.75 2.35, ⁻0.073, 0.3333, or 2.142857. It also includes all the irrational numbers such as π, √2 etc. Every real number corresponds to a point on the number line.

    case all = "" // all of the above

}

public enum AMInputType:Equatable {

    case list(AMSelectionStyle)

    case number(AMNumberType)

    case calender(AMCalenderMode)

    case freeText

    case boolian

    case picklist

    

   public static func ==(lhs: AMInputType, rhs: AMInputType) -> Bool {

        switch (lhs, rhs) {

        case (let .list(a1), let .list(a2)):

            if a1 == .any || a2 == .any {

                return true

            }else if a1 == .single && a2 == .single {

                return true

            }else if a1 == .multiple && a2 == .multiple {

                return true

            }

            return a1 == a2

        case (let .number(a1), let .number(a2)):

            if a1 == .all || a2 == .all {

                return true

            }else if a1 == .natural && a2 == .natural {

                return true

            }else if a1 == .whole && a2 == .whole {

                return true

            }else if a1 == .integer && a2 == .integer {

                return true

            }else if a1 == .rational && a2 == .rational {

                return true

            }else if a1 == .real && a2 == .real {

                return true

            }

            return a1 == a2

        case  (let .calender(a1), let .calender(a2)):

            if a1 == .any || a2 == .any {

                return true

            }else if a1 == .date && a2 == .date {

                return true

            }else if a1 == .time && a2 == .time {

                return true

            }else if a1 == .dateTime && a2 == .dateTime {

                return true

            }

            return a1 == a2

        case (.freeText, .freeText):

            return true

        case (.boolian, .boolian):

            return true

        case (.picklist, .picklist):

            return true

        default:

            return false

        }

    }

}

func compareEnum(inputType:AMInputType)->Bool{

    switch inputType {

    case .number(.all):

        print("Switch case number check Successful")

        return true

    case .calender(.any):

        print("Switch case calender check Successful")

        return true

    case .list(.any):

        print("Switch case list check Successful")

        return true

    default:

        print("Switch case check Failed")

        return false

    }

}

//Comparasion fails

compareEnum(inputType: .number(.real))

//Compatisation succeeded when enum parameters values are same

compareEnum(inputType: .number(.all))

//If condition succeeds

if AMInputType.number(.all) == AMInputType.number(.real) {

    print("If Condition number check Successful")

}

Seems Swift prefers pattern matching rather than overridden == operator when you use a value of enum with associated value.

You can try something like this:

extension AMInputType {
    public struct Matcher {
        fileprivate let inputType: AMInputType
        public init(_ inputType: AMInputType) {
            self.inputType = inputType
        }
    }

    public static func ~= (lhs: AMInputType, rhs: Matcher) -> Bool {
        return lhs == rhs.inputType
    }
}

func compareEnum(inputType: AMInputType) -> Bool {
    switch AMInputType.Matcher(inputType) { //<-
    case .number(.all):
        print("Switch case number check Successful")
        return true
    case .calender(.any):
        print("Switch case calender check Successful")
        return true
    case .list(.any):
        print("Switch case list check Successful")
        return true
    default:
        print("Switch case check Failed")
        return false
    }
}
How to override switch case behaviour like we override operator == with Equatable protocol
 
 
Q