Skip to content

Visibility & modifiers

View Markdown

Set visibility inside a declaration block using Visibility.Public, Visibility.Internal, Visibility.Protected, or Visibility.Private. If you leave it unset, Prosa.kt emits no visibility keyword.

import com.alexstyl.prosakt.ktFile
import com.alexstyl.prosakt.Visibility
val source = ktFile {
ktClass("GreetingService") {
visibility = Visibility.Internal
ktValue("prefix") {
visibility = Visibility.Private
initializer {
literal("Hello")
}
}
}
}

Generated Kotlin:

internal class GreetingService {
private val prefix = "Hello"
}

Set modifiers to a list of Kotlin modifier keywords, such as open, data, override, or abstract. The keywords are emitted in the order you supply.

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

Generated Kotlin:

abstract class GreetingProvider {
abstract fun greeting(): String
}

An abstract function is emitted without a body, and cannot also configure body {}. Choose modifiers that are valid for the declaration and its context; Prosa.kt does not act as a Kotlin compiler.