I am trying to dynamically create a class instance based type using generics, however I am facing some strange behavior. In example 1, everything works, but in Example 2, if i pass Test.self to the generic function, it doesn't work. The type is the same, everything is the same, i don't understand why.
class Test{
required init(x: Int){
// Do Something
}
}
class Builder{
init(){
}
func use<T>(test2: T.Type) -> Void{
test2(x: 10) // Error: T cannot be constructed because it has no accessible initializers
}
}
// Example 1:
let test1 = Test.self
test1(x: 10)
// Example 2:
let builder = Builder()
builder.use(Test.self)
It's because
T
is not of TypeTest
. To fix this: