1

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?

Milos
  • 2,728
  • 22
  • 24
  • I don't have the time now to check this, but do let me know if you're confident it is a duplicate and I'll happily delete the question... – Milos Apr 24 '15 at 12:13
  • This seems to be a duplicate of http://stackoverflow.com/questions/25838032/optional-binding-succeeds-if-it-shouldnt. It happens only in Debug mode, and the same workaround applies here as well. I had closed it as a duplicate but inadvertently reopened the question. Sorry for the confusion. – Martin R Apr 26 '15 at 13:48

0 Answers0