Skip to content

Interfaces

View Markdown

Use ktInterface() to declare an interface. Add functions with ktFunction(). Set modifiers = listOf("abstract") on a function to generate a declaration without a body.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktInterface("GreetingProvider") {
ktFunction("greeting") {
modifiers = listOf("abstract")
returns {
type {
reference("String")
}
}
}
}
}

Generated Kotlin:

interface GreetingProvider {
abstract fun greeting(): String
}

Use returns {} to configure the return type and type {} with reference() to name that type. The explicit abstract modifier is needed here because ordinary functions receive a body by default.

Use body {} on an interface function to generate a default implementation. Leave abstract out of modifiers when providing a body.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktInterface("GreetingProvider") {
ktFunction("greeting") {
returns {
type {
reference("String")
}
}
body {
literal("Hello")
}
}
}
}

Generated Kotlin:

interface GreetingProvider {
fun greeting(): String {
return "Hello"
}
}