'Enum with identical cases names with associated values of different types

The following Swift code compiles:

enum GraphDataSource  {
    case array(data: [Double], start: Double?, step: Double?)
    case pairs(XYValues: [Double: Double])
    case pairs(dateValues: [Date: Double])
    case function((Double) -> Double?)

    func localizedName() -> String {
        // TODO: Create localizable strings
        return NSLocalizedString(Mirror(reflecting: self).children.first?.label ?? "", comment: "")
    }
}

It has two enum cases named pairs. But when I try to extract associated value, it turns out that I can't choose the one I want.

    var graphData = GraphDataSource.function(sin)

    switch graphData {
    case .pairs(dateValues: let vals):
        vals.keys.forEach({print($0)})
    case .pairs(XYValues: let xy): // without this case everyting compiles OK
        xy.keys.forEach({print($0)})
    default:
        break
    }

The error is: "Tuple pattern element label 'XYValues' must be 'dateValues'". Is this normal? Feels like compiler should either disallow cases of the same name or allow to switch on both.



Solution 1:[1]

There are two workaround to pass different types of associated values throw the one case:

  1. Use Any type for an associated value and then dynamically type check/cast it:
enum DataType {
    case data(_ value: Any)
}

func process(_ type: DataType) {
    switch type {
    case .data(let value):
        if value is Int {
            print("Int value - \(value)")
        }
        else if value is String {
            print("String value - \(value)")
        }
    }
}

process(.data(10)) // Outputs: Int value - 10
process(.data("Text")) // Outputs: String value - Text
  1. Use an additional enum to specify a needed type:
enum ValueType {
    case int(_ value: Int)
    case string(_ value: String)
}

enum DataType {
    case data(_ value: ValueType)
}

func process(_ type: DataType) {
    switch type {
    case .data(let value):
        switch value {
        case .int(let value):
            print("Int value - \(value)")
        case .string(let value):
            print("String value - \(value)")
        }
    }
}

process(.data(.int(10))) // Outputs: Int value - 10
process(.data(.string("Text"))) // Outputs: String value - Text

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iUrii