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

Add warning for ambiguous enum value assignment

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
    • Environment:

      Description

      I've been caught off-guard by the value that's chosen by the compiler when inferring the type to assign to an enum value. This can be especially surprising because nil is shorthand for Optional.none.

      For example, if I have this enum:

      enum Coverage {
          case all      ///< everything is covered
          case partial  ///< only partially covered
          case none     ///< nothing is covered
      }
      
      let myCoverageA: Coverage? = .all
      let myCoverageB: Coverage? = .none
      

      Because myCoverageA gets a value of .some(.all), you might expect the value of myCoverageB to be .some(.none), but in fact it is .none.

      I think this would also come up in a double-Optional. For example:

      let str: String?? = nil
      // is it `.none` or `.some(.none)`?
      

      Expect

      I think it would be nice to have a compiler warning in ambiguous cases like this.

      Notes

      I wrote up a slightly more detailed example of this here: http://kelan.io/2016/type-inferior-ence/

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                kelan Kelan Champagne
              • Votes:
                2 Vote for this issue
                Watchers:
                11 Start watching this issue

                Dates

                • Created:
                  Updated: