I have this question except for Swift. How do I use a Type variable in a generic?
I tried this:
func intType() -> Int.Type {
retu
There is a way and it's called generics. You could do something like that.
class func foo() {
test(Int.self)
}
class func test(t: T.Type) {
var arr = Array()
}
You will need to hint the compiler at the type you want to specialize the function with, one way or another. Another way is with return param (discarded in that case):
class func foo() {
let _:Int = test()
}
class func test() -> T {
var arr = Array()
}
And using generics on a class (or struct) you don't need the extra param:
class Whatever {
var array = [T]() // another way to init the array.
}
let we = Whatever()