Cannot explicitly specialize a generic function
Originator: | nacho4d | ||
Number: | rdar://20583619 | Date Originated: | 2015/0417 |
Status: | Open | Resolved: | |
Product: | Product Version: | ||
Classification: | Serius bug | Reproducible: | Always |
Summary: The compiler does not allow me to use a generic function of the form: func myFunction<T>() -> Void {} Steps to Reproduce: 1. Write a function of above form: func myFunction<T>() -> Void {} 2. Call it: myFunction<MyObject>() 3. Compile it OR Just execute the attached code in a playground :) Expected Results: It compiles without issues Actual Results: Error: Cannot explicitly specialize a generic function Version: Xcode 6.3 swift 1.2 Notes: Same error on swift1.1 - Not only me: http://stackoverflow.com/questions/27965439/cannot-explicitly-specialize-a-generic-function http://stackoverflow.com/questions/28726937/how-do-i-call-a-generic-swift-function-when-none-of-the-arguments-provides-the-g https://twitter.com/dangerdave/status/490385202170982401 Attached code: import Foundation protocol Registrable: NSObjectProtocol { static func registerIfNeeded() static func unregister() init() } class Something: NSObject, Registrable { static func registerIfNeeded() { } static func unregister() { } override required init() { } } func doMyThing<T: Registrable>(whatever: Int) { // Before doing my stuff I make sure the class is registered T.registerIfNeeded() // Now I can do my thing var t = T() } //: No way the compiler can know about `Something` unless it is passed explicitly. However it does not work explicitly too: **ERROR** ***Cannot explicitly specialize a generic function*** doMyThing<Something>(123) func doMyThing2<T: Registrable>(index: Int) -> T { T.registerIfNeeded() var t = T() return t } doMyThing2(123) as Something // This works because the compiler got a hint
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!