Xcode-beta (7A192o): In Swift, allow explicit specialization of generic functions

Originator:david.w.hart
Number:rdar://22471737 Date Originated:28-Aug-2015 10:41 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (7A192o)
Classification:Other Bug Reproducible:Always
 
Summary:
I’d like Swift to allow me to explicitly specialize generic functions, allowing me to write functions which use the generic type only in their return value.

Steps to Reproduce:
I have two example, one small which demonstrates the problem but is a bit non-sensical:

extension Int {
	func cast<T>() -> T { return self as! T }
}

let value = 5
let casted = 5.cast<Double>()

And one more complex that demonstrates what I’m actually trying to achieve in a project:

class ObjectBundle<T: NSObject> {
	var objects: [String:T] = [:]
	
	func add(object: T) {
		self.objects[NSStringFromClass(object.dynamicType)] = object
	}
	
	func get<U: NSObject>() -> U? {
		return self.objects[NSStringFromClass(U.self)]
	}
}

class MyBaseClass: NSObject {
	var myBaseProp: Int = 0
}
class MyClass: MyBaseClass {
	var myProp: Int = 0
}

var bundle = ObjectBundle<MyBaseClass>()
bundle.add(MyBaseClass())
bundle.add(MyClass())
let obj = bundle.get<MyClass>()

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!