Values and Variables
View MarkdownDeclare values and variables
Section titled “Declare values and variables”Use ktValue() for val and ktVariable() for var. Pass the property name as the argument. Use initializer {} to define its initial value.
import com.alexstyl.prosakt.ktFile
val source = ktFile { ktValue("greeting") { initializer { literal("Hello") } } ktVariable("requestCount") { initializer { literal(0) } }}Generated Kotlin:
val greeting = "Hello"var requestCount = 0Specify a type
Section titled “Specify a type”Use type {} directly inside the property block to specify its type. Use reference() inside it to name the type.
import com.alexstyl.prosakt.ktFile
val source = ktFile { ktValue("greeting") { type { reference("String") } initializer { literal("Hello") } }}Generated Kotlin:
val greeting: String = "Hello"Add a getter
Section titled “Add a getter”Use getter {} to configure a custom getter and body {} inside it for the implementation. The getter returns its last expression.
import com.alexstyl.prosakt.ktFile
val source = ktFile { ktValue("greeting") { type { reference("String") } getter { body { literal("Hello") } } }}Generated Kotlin:
val greeting: String get() { return "Hello" }Use delegate {} instead of initializer {} for a delegated property. A property cannot combine a delegate with an initializer or a custom getter.