kotlin-dsl
Kotlin DSL - type-safe builders, Gradle DSL, @DslMarker
When & Why to Use This Skill
This Claude skill empowers developers to master Kotlin DSL (Domain-Specific Language) development, focusing on creating type-safe builders and sophisticated Gradle configurations. By leveraging core Kotlin features like @DslMarker, it helps eliminate scope pollution and ensures compile-time safety, significantly improving code readability and maintainability in complex projects.
Use Cases
- Scenario 1: Architecting type-safe builders for complex data structures or UI layouts (like HTML or Compose) to replace verbose and error-prone boilerplate code.
- Scenario 2: Developing custom Gradle convention plugins and DSLs to streamline build logic across multi-module Android or Backend projects.
- Scenario 3: Implementing hierarchical configuration systems with strict scope control using @DslMarker to prevent invalid property access in nested DSL blocks.
- Scenario 4: Migrating legacy Groovy-based Gradle scripts to Kotlin DSL for better IDE support, auto-completion, and refactoring capabilities.
| name | kotlin-dsl |
|---|---|
| description | Kotlin DSL - type-safe builders, Gradle DSL, @DslMarker |
| version | "1.0.0" |
| sasmp_version | "1.3.0" |
| bonded_agent | 07-kotlin-dsl |
| bond_type | PRIMARY_BOND |
| timeout_ms | 30000 |
| max_attempts | 3 |
| backoff | exponential |
| initial_delay_ms | 1000 |
| - name | immutable |
| type | boolean |
| validation | "^(gradle|custom|html|config)$" |
| default | true |
| level | info |
| events | [skill_invoked, type_loaded, error_occurred] |
Kotlin DSL Skill
Build type-safe DSLs with Kotlin's language features.
Topics Covered
@DslMarker for Scope Control
@DslMarker
annotation class HtmlDsl
@HtmlDsl
class HTML { fun body(block: Body.() -> Unit) { } }
@HtmlDsl
class Body { fun p(text: String) { } }
// Usage - scoped correctly
html { body { p("Text") } }
Gradle Convention Plugin
// kotlin-library.gradle.kts
plugins { kotlin("jvm"); `java-library` }
kotlin { jvmToolchain(17) }
dependencies {
testImplementation(kotlin("test"))
testImplementation("io.mockk:mockk:1.13.9")
}
tasks.test { useJUnitPlatform() }
Type-Safe Config Builder
@ConfigDsl
class ServerConfig private constructor(val host: String, val port: Int) {
class Builder {
var host = "localhost"
var port = 8080
fun build() = ServerConfig(host, port)
}
}
fun serverConfig(block: ServerConfig.Builder.() -> Unit) =
ServerConfig.Builder().apply(block).build()
// Usage
val config = serverConfig { host = "api.example.com"; port = 443 }
Troubleshooting
| Issue | Resolution |
|---|---|
| Scope pollution | Add @DslMarker annotation |
| Gradle cache stale | Run ./gradlew --stop |
Usage
Skill("kotlin-dsl")