Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is a Swift compiler bug. See SR-10077.

over 4 years ago · Santiago Trujillo Report

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!