kotlinkotlinpoet

KotlinPoet - No brackets for interface methods


I'm generating interfaces with KotlinPoet with the following code

val funspec = FunSpec.builder("test").build()
val interfacespec = TypeSpec.interfaceBuilder("Test").addFunction(funspec).build()

This generates the following code:

interface Test {
      fun test() {
      }
}

The function test() has a default implementation (has brackets). Is there any way to remove the default implementation (remove the brackets)?


Solution

  • Note that interface methods must always be ABSTRACT. The modifier is necessary when defining the interface... But these modifiers are omitted when the code is generated. These are the defaults so we don't need to include them for kotlinc's benefit!

    So simply add .addModifiers(KModifier.ABSTRACT) to your funspec.