Swift generic function loses type information

Originator:ivan
Number:rdar://17297399 Date Originated:2014-06-13
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode Version 6.0 (6A215l)
Classification:Serious Bug Reproducible:Yes
 
Summary:
I was trying to implement double-dispatching for which I need correct type preservation. But if I'm right this is broken as my dispatch generic function doesn't preserve enough of the type information to perform correct overloading.

Steps to Reproduce:
Paste the following in a playground:

class Base {}
class Derived : Base {}

class Dispatcher {
    
    func dispatch<T: Base>(t: T) {
        // At this point compiler knows that t is an instance of T which *might* be Base or derived from Base.
        // Yet its overload resolution always treats is as Base not as T
        accept(t)
    }
    
    func accept(t: Base) {
        println("accept(Base)")
    }
    func accept(t: Derived) {
        println("accept(Derived)")
    }
    
}

var base = Base()
var derived = Derived()
var dispatcher = Dispatcher()
dispatcher.dispatch(base)
dispatcher.dispatch(derived)


Expected Results:
accept(Base)
accept(Derived)

Actual Results:
accept(Base)
accept(Base)

Version:
Xcode Version 6.0 (6A215l)
OS X 10.9.3 (13D65)


Notes:
I haven't figured out any workaround. I tried explicitly casting "t" to "T" ("t as T") but it didn't help.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!