Uploaded image for project: 'Swift'
  1. Swift
  2. SR-7799

Matching optional enums against non-optional values

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None

      Description

      We can match an optional Int against a non-optional value:

      let x: Int? = 1
      
      switch x {
      case 1: print("one")
      default: print("default")
      }
      

      But the same does not compile with an optional enum:

      enum MyEnum { case a, b }
      let e: MyEnum? = .a
      
      switch e {
      case .a: print("a") // error: Enum case 'a' not found in type 'MyEnum?'
      case .some(.a): print("some a") // OK
      default: print("default")
      }
      

      Tested with Xcode 9.3.1, with both the default toolchain and the Swift 4.2 snapshot from May 20, 2018.

      Related discussion in the Swift forum: Matching optionals in a switch statement, where Joe Groff said:

      It ought to compile as well. That's a bug.

        Attachments

          Activity

            People

            • Assignee:
              suyashsrijan Suyash Srijan
              Reporter:
              martin Martin
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: