swiftlang/swift

[SR-4451] type(of:) behaviour change from Swift 3.0.2 to Swift 3.1.

Open

#47,028 opened on Mar 31, 2017

View on GitHub
 (8 comments) (0 reactions) (0 assignees)Swift (69,989 stars) (10,719 forks)batch import
bugcompilergood first issuetype checker

Description

Previous ID SR-4451
Radar rdar://problem/31404284
Original Reporter razvandub (JIRA User)
Type Bug

Attachment: Download

Votes 0
Component/s Compiler
Labels Bug, StarterBug, TypeChecker
Assignee None
Priority Medium

md5: 59a922e46e7486ff5d399c2af0e0d2d8

Issue Description:

It seems that the behaviour of "type(of: )" has changed from Swift 3.0.2 to Swift 3.1 w.r.t. optional Any? closure parameters referenced as Any.

"type(of: oItem) as? T.Type" below returns "__NSCFString.Type" when using Swift 3.0.2 compiler and "nil" when using Swift 3.1 compiler.

import Foundation

internal extension Set where Element: NSObject {
    
    internal func items <T: NSObject> (ofType itemType: T.Type) -> Set<T>? {
        return NSSet(set: self).filtered(using: NSPredicate { (oItem: Any /* Note Any instead of Any? */, _) -> Bool in
            return nil != (type(of: oItem) as? T.Type)  // Swift 3.0.2 => true, Swift 3.1 => false.
            //                        return oItem is T // Swift 3.0.2 => true, Swift 3.1 => true.
        }) as? Set<T>
    }
}

let stringsOfTypeNSObject = (["1", "2"] as Set<NSString>).items(ofType: NSObject.self)

print(stringsOfTypeNSObject ?? []) // Swift 3.0.2 => [1, 2], Swift 3.1 => [].

Contributor guide

[SR-4451] type(of:) behaviour change from Swift 3.0.2 to Swift 3.1. · swiftlang/swift#47028 | Good First Issue