Looking at the Kotlin grammar , it seems that this is not possible at the moment. For primary designers, type parameters denote class type parameters:
class (used by memberDeclaration, declaration, toplevelObject) : modifiers ("class" | "interface") SimpleName typeParameters? primaryConstructor? (":" annotations delegationSpecifier{","})? typeConstraints (classBody? | enumClassBody) ;
There are no type parameters for secondary constructors:
secondaryConstructor (used by memberDeclaration) : modifiers "constructor" valueParameters (":" constructorDelegationCall)? block ;
However, the constructor is just a special function. If we do not use the constructor instead, but a function of our own, we can come up with the following:
class Test { interface I1 interface I2 private val mI1: I1 private val mI2: I2 internal constructor(host: I1, host2: I2) { mI1 = host mI2 = host2 } companion object { fun <T> create(host: T): Test where T : Test.I1, T : Test.I2 { return Test(host, host) } } } fun <T> test(host: T): Test where T : Test.I1, T : Test.I2 { return Test(host, host) }
Now we can call Test.create(host) or test(host) to create an instance.
source share