Skip to content

Use ktClass() inside ktFile {} to declare a class. Pass the class name as its argument.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktClass("GreetingService") {
}
}

Generated Kotlin:

class GreetingService

Use constructor {} inside ktClass {} to configure the primary constructor. Use parameter() inside it to add a parameter, then type {} and reference() to specify its type.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktClass("GreetingService") {
constructor {
parameter("prefix") {
type {
reference("String")
}
}
}
}
}

Generated Kotlin:

class GreetingService(prefix: String)

Use property {} inside a constructor parameter to generate a val property. Set mutable = true inside that block to generate var instead. Set modifiers = listOf("data") on the class to generate a data class.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktClass("User") {
modifiers = listOf("data")
constructor {
parameter("name") {
type {
reference("String")
}
property {
}
}
parameter("active") {
type {
reference("Boolean")
}
default {
literal(true)
}
property {
mutable = true
}
}
}
}
}

Generated Kotlin:

data class User(val name: String, var active: Boolean = true)

Use visibility = Visibility.Private inside property {} to make a constructor property private. Call constructor {} once per class; add all primary-constructor parameters inside it.

Use ktFunction(), ktValue(), or ktVariable() inside the class block to add members. Use body {} inside a function to define its implementation.

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

Generated Kotlin:

class GreetingService {
fun greeting(): String {
return "Hello"
}
}

To add declaration keywords such as open, use modifiers; see Visibility & modifiers.