swiftlang/swift

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

Open

#47 028 ouverte le 31 mars 2017

Voir sur GitHub
 (8 commentaires) (0 réactions) (0 assignés)Swift (10 719 forks)batch import
bugcompilergood first issuetype checker

Métriques du dépôt

Stars
 (69 989 stars)
Métriques de merge PR
 (Merge moyen 8j 17h) (510 PRs mergées en 30 j)

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 => [].

Guide contributeur