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

No error for using 'as' patterns with CF types

    XMLWordPrintable

    Details

      Description

      import Foundation
      
      let x = ([1: 2] as NSDictionary) as Any
      
      switch x {
      case let b as CFBoolean:
          print("it's a cfbool: \(b)")
          
      default:
          print("n/a")
      }
      

      We should warn here that we can't actually do checked downcasts for CF types, like we do with as?.

      import Foundation
      let x = ([1: 2] as NSDictionary) as Any
      let y = x as? CFDictionary // error: conditional downcast to CoreFoundation type 'CFDictionary' will always succeed
      

      We can't make it an error because that would break source compatibility, but at least a warning will work. The best workaround today (and a possible fix-it):

      switch x {
      case let rawB:
          let b = rawB as! CFBoolean
          print("it's a cfbool: \(b)")
          
      default:
          print("n/a")
      }
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            jrose Jordan Rose
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: