kotlin
Use when writing Kotlin on the JVM with Gradle — command-line tools with clikt, data classes and sealed hierarchies, fair rotation or scheduling algorithms, kotlin.test or JUnit 5, and runnable distri
SKILL.mdskills/languages/kotlin/SKILL.md
GitHubKotlin (JVM)
Default: current Kotlin with the Gradle wrapper and Kotlin DSL, JVM toolchain pinned, clikt for CLIs, kotlin.test on JUnit 5, application plugin.
Setup
gradle init --type kotlin-application --dsl kotlin; commit the wrapper.kotlin { jvmToolchain(21) }so the JDK is chosen by the build, not by the machine.allWarningsAsErrors = trueincompilerOptions.
CLI with clikt
class Rotate : CliktCommand() {
private val members by option("--member").multiple(required = true)
private val weeks by option().int().default(4)
override fun run() = echo(plan(members, weeks).render())
}
fun main(args: Array<String>) = Rotate().main(args)
Keep run() thin; plan(...) is a pure function you test directly. (Check the clikt version you use: recent major versions changed how help text is declared.)
Example: fair task rotation
- Give each task a weight (effort) and keep a history of who did what.
- Assign each slot to the person with the lowest accumulated weight who is available, breaking ties deterministically (e.g. least recently assigned, then name).
- Report the resulting totals per person and the maximum difference; that number is the fairness metric to test against.
- Never use unseeded randomness for tie-breaking — the same input must give the same plan.
Language habits
data classfor values,sealed interfacefor closed variants with exhaustivewhen.- Prefer
valand immutable collections (listOf,buildList); exposeList, notMutableList. - Use
require/checkfor preconditions with messages. - Nullable types are part of the signature; avoid
!!outside tests.
Testing
kotlin.testassertions with JUnit 5; parameterised tests for tables of members, weights and history.- Test the invariant (max difference ≤ largest single weight) rather than one exact schedule when several are equally fair.
Verification loop
./gradlew build— compiles with warnings as errors and runs tests../gradlew run --args="--member a --member b"../gradlew installDistand run the generated script.
Pitfalls
- Platform types from Java APIs (
String!) bypass null checks; annotate or check at the boundary. ==vs===:==isequals,===is identity.- Data class
copyis shallow; nested mutable objects are shared. - Gradle Kotlin DSL errors surface at configuration time; run
./gradlew helpto see them quickly. - Kotlin and Gradle plugin versions must be compatible; upgrade them together.