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

Missing conditional cast fix-it

    XMLWordPrintable

    Details

      Description

      let any: Any = 1
      if let int = any as Int {
        // ...
      }
      
      $ swiftc missing-checked-cast-fixit.swift
      missing-conditional-cast-fixit.swift:2:18: error: 'Any' is not convertible to 'Int'; did you mean to use 'as!' to force downcast?
      if let int = any as Int {
                   ~~~~^~~~~~
                       as!
      

      There's only a fix-it for force downcasting (as!). It could also make sense to have one for conditional downcasting (as?), I ran into such a case in practice. Example:

      missing-checked-cast-fixit.swift:2:18: error: 'Any' is not convertible to 'Int'; did you mean to use 'as?' to conditionally downcast?
      if let int = any as Int {
                   ~~~~^~~~~~
                       as?
      

        Attachments

          Activity

            People

            Assignee:
            luciano Luciano Almeida
            Reporter:
            danzheng Dan Zheng
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: