Swift: Generic extensions in libraries currently broken
Just saving you some time
If you were thinking of creating a Swift framework and writing some unit tests, you should be aware of a bug. You cannot currently extend a built-in or standard library generic type with any properties or methods that require the type's generic parameters.
In other words, create a framework project in Swift and add this to a swift file:
extension Dictionary {
func OK() { }
func NotOK(a:KeyType) {
}
}
Now in one of your unit tests, try this (after importing your framework project):
let dict = ["abc": 5, "xyz": 3]
dict.OK()
dict.NotOK("") //oops, linker error
You can use the extension method OK
just fine, but as soon as you try to use NotOK
, the linker will barf with an Undefined symbols for architecture
error. That's because your unit tests are actually a separate product (aka binary). My theory is this is due to name mangling where the framework generates a different name than the unit test binary. I also strongly suspect this would happen for any type of dynamic library involving Swift generic classes but I haven't confirmed that.
For the Apple folks, rdar://17264362 has been filed
This blog represents my own personal opinion and is not endorsed by my employer.