1

I'm trying to generate an object which implements an interface with type parameter like the following example:

object HelloWorld : Feature<Intent>

I can generate the object that implements my interface like the following code:

val typeSpecBuilder = TypeSpec.objectBuilder("HelloWorld")
typeSpecBuilder.addSuperinterface(
      ClassName(
               "com.example.mylib",
               "Feature"
      )

How can I pass the type argument to the interface?

savepopulation
  • 11,736
  • 4
  • 55
  • 80

2 Answers2

4

You can use parameterizedBy() method to use generic type. If it is not detected by IDE, you can import manually.

mertsimsek
  • 266
  • 2
  • 10
2

Import plusParameter manually:

import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter

And use like below:

val typeSpecBuilder = TypeSpec.objectBuilder(feature.featureName)
typeSpecBuilder.addSuperinterface(
      ClassName(
            "com.raqun.icarus.core",
            "Feature"
      ).plusParameter(ClassName("com.example.myawesomeclass", "MyAwesomeClass"))
)
savepopulation
  • 11,736
  • 4
  • 55
  • 80