I write the following code but it doesn't works because I get an unresolved reference error.
class Chromosome { constructor() {} companion object { fun newInstance(): Chromosome { return Chromosome() } } } class Population<T>(size: Int) where T: Chromosome { var population: Array<T> = Array(size, { _ -> T.newInstance() }) } I want a generic type T that extends a certain class or interface. I need to call a static factory methods because I cannot write T() in Array class constructor. Are there any way to wrap Array class calling a default constructor?
I tried with a different approach but it still not works. This time I get an Cannot use 'T' as reified type parameter. Use a class instead error.
class Population<T>(size: Int, init: () -> T) where T: Chromosome { var population: Array<T> = Array(size, { _ -> init() }) }
ClassYisClass Y. Right?