The problem is reproducible in the playgrounds as follows:
import Cocoa
func isOfType <T: NSWindow> (type: T.Type) -> NSWindow -> Bool {
return { $0 is T }
}
class MyWindow: NSWindow {}
let nsWindow = NSWindow()
let myWindow = MyWindow()
let isOfTypeMyWindow = isOfType(MyWindow)
isOfTypeMyWindow(nsWindow) //--> true
isOfTypeMyWindow(myWindow) //--> true
Am I wrong to expect the first one of these to return false
? This is what happens if we remove the type constraint from the function signature:
import Cocoa
func isOfType <T> (type: T.Type) -> NSWindow -> Bool {
return { $0 is T }
}
class MyWindow: NSWindow {}
let nsWindow = NSWindow()
let myWindow = MyWindow()
let isOfTypeMyWindow = isOfType(MyWindow)
isOfTypeMyWindow(nsWindow) //--> false
isOfTypeMyWindow(myWindow) //--> true
Which is the behaviour I expected to begin with. The same happens with the two versions of an even simpler case:
func isOfType <T: NSWindow> (type: T.Type, window: NSWindow) -> Bool {
return window is T
}
and
func isOfType <T> (type: T.Type, window: NSWindow) -> Bool {
return window is T
}
Am I wrong to expect that T
should be a subtype of NSWindow
in both cases when I pass in a subtype? – with or without the constraint, which is only there meant to prevent arguments of non window types...
And if you stick a println
in there:
func isOfType <T: NSWindow> (type: T.Type, window: NSWindow) -> Bool {
println("\(window) is \(T.self) == \(window is T)")
return window is T
}
isOfType(MyWindow.self, nsWindow)
You get this unbelievable printout in the playground console:
//--> <NSWindow: 0x7f9d51584b40> is __lldb_expr_496.MyWindow == true
Is this Apple's bug, or have I missed something obvious to everyone else?