Reputation: 767
I have this code that represents a non-generic Struct:
struct rum {
var prop = "Hello"
}
let's suppose that I need to create an extension for this Struct to receive generic types. For this, I try this code:
extension rum <T>{
func release(bo: T){
println(T)
}
}
But Xcode gives me many errors, that rum does not have a generic type. But the idea is to create a non-generic Struct and transform it into a generic. Is it possible?
Upvotes: 0
Views: 182
Reputation: 726669
If rum
were a class, you would get more flexibility, because you could derive a generic class from it, and define an extension on that generic class. However, struct
is a value type, so you cannot derive a class from it.
Since rum
is not generic struct
, you need to make the extension method generic, like this:
extension rum {
func release<T>(bo: T){
println(prop)
println(bo)
}
}
Unfortunately, this means that all extension methods that you wish to be generic need to be decorated with <T>
.
Upvotes: 2