Objects
View MarkdownDeclare an object
Section titled “Declare an object”Use ktObject() to declare a singleton object. Use the same member builders as a class, such as ktValue() and ktFunction().
import com.alexstyl.prosakt.ktFile
val source = ktFile { ktObject("Defaults") { ktValue("greeting") { initializer { literal("Hello") } } }}Generated Kotlin:
object Defaults { val greeting = "Hello"}Add a companion object
Section titled “Add a companion object”Use companionObject {} inside ktClass {} or ktInterface {}. Pass a name to companionObject() if you want a named companion.
import com.alexstyl.prosakt.ktFile
val source = ktFile { ktClass("GreetingService") { companionObject { ktValue("DEFAULT_GREETING") { modifiers = listOf("const") initializer { literal("Hello") } } } }}Generated Kotlin:
class GreetingService { companion object { const val DEFAULT_GREETING = "Hello" }}