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

Confusing unwrapping diagnostic when using implicit member access on Optionals.

    XMLWordPrintable

    Details

      Description

      struct Object {
          init?() {
              return nil
          }
      }
      
      struct Test {
          let id: Int
          let object: Object?
      }
      
      Test(id: 1, object: nil)
      Test(id: 1, object: Object())
      Test(id: 1, object: .init()) // this will fail
      

       

      As you can see, the Test Struct contains an id along with an optional Object property.

      Passing the Object's .init() in the Test initializer will lead to the error: 

      Value of optional type 'Object?' must be unwrapped to a value of type 'Object'
      

      This diagnostic doesn't make it clear what the issue is.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              iDevid Davide Sibilio
              Reporter:
              iDevid Davide Sibilio
              Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: