Search code examples
swiftintrospection

Passing a swift Class to Generic Function


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)

Solution

  • It's because T is not of Type Test. To fix this:

    class Builder{
      init(){
      }
    
      // T has to be of type Test or is a subclass of Test
      func use<T: Test>(test2: T.Type) -> Void{
        test2(x: 10)
      }
    }