If I had a generic class like this:
class Generic<T: Any> {
var object: T
init(object: T) {
self.object = object
}
}
I could access the object from the initializer like this:
Generic(object: "Hello World").object.count // 11
As this generic class works for Any
type, how would I extend all types to return a object of that generic class?
I mean something like this:
extension Any {
var generic: Generic<Self> {
Generic(object: self)
}
}
So I can call:
"Hello World".generic.object.count // 11
I am confused with all the Any
, AnyObject
, Self
, self
, Type
and so on.
UPDATE:
For example, how would I extend all Comparable
with the computed properties array: Array<Comparable>
? I am thinking of something like:
extension Comparable {
var array: Array<Comparable> {
Array(self)
}
}