Skip to content

Language Kotlin

Oliver Atkinson edited this page May 31, 2026 · 1 revision

Kotlin

The Kotlin generator emits a stand-alone .kt file. It does not require a Kotlin Lexicon runtime package.

Generate Kotlin

swift run lexicon-generate commerce.lexicon --type kotlin -o app/src/main/kotlin/com/example/commerce/Lexicon

This writes:

app/src/main/kotlin/com/example/commerce/Lexicon.kt

Add a package declaration yourself if your project requires one:

package com.example.commerce

Place it at the top of the generated file or wrap generation with a build step that prepends it.

Generated Shape

Generated Kotlin defines:

  • I, the shared vocabulary interface.
  • TypeLocalized, for localized display strings.
  • SourceCodeIdentifiable, for exact IDs.
  • L, the base class.
  • one generated data class per concrete lemma.
  • one generated interface per semantic type.
  • extension properties for children and inherited children.

Example access:

val submit = commerce.api.order.submit
val button = commerce.ui.checkout.button.primary

println(submit.identifier)
println(button.localized)

The identifier value is the exact lemma ID.

Reserved Words and Unusual Names

Kotlin generated accessors use backticks:

val type = commerce.`type`

This keeps generated code valid when a lemma name conflicts with a Kotlin keyword.

Synonyms

A synonym becomes a typealias or property pointing at the resolved source. Given:

state:
	enabled:
	active:
	= enabled

Use the generated accessors as normal:

val enabled = commerce.state.enabled
val active = commerce.state.active

check(enabled.identifier == active.identifier)

Gradle Integration

Use an Exec task when the Kotlin project can run Swift during generation:

tasks.register<Exec>("generateLexicon") {
    workingDir = rootProject.projectDir
    commandLine(
        "swift", "run", "lexicon-generate",
        "lexicons/commerce.lexicon",
        "--type", "kotlin",
        "-o", "app/src/main/kotlin/com/example/commerce/Lexicon"
    )
}

tasks.named("compileKotlin") {
    dependsOn("generateLexicon")
}

If Swift should not be required in normal Kotlin builds, generate the file in a docs/tooling workflow and commit the generated .kt.

Practical Use

Use generated Kotlin for:

  • stable IDs in analytics events
  • typed feature-flag names
  • shared UI state vocabulary
  • data-mapping keys that must match API and documentation vocabulary

Prefer identifier for persistence, logging, and protocol boundaries. Prefer generated accessors in source code so refactors fail at compile time.

Clone this wiki locally