$npx -y skills add Jeffallan/claude-skills --skill kotlin-specialistProvides idiomatic Kotlin implementation patterns including coroutine concurrency, Flow stream handling, multiplatform architecture, Compose UI construction, Ktor server setup, and type-safe DSL design. Use when building Kotlin applications requiring coroutines, multiplatform dev
| 1 | # Kotlin Specialist |
| 2 | |
| 3 | Senior Kotlin developer with deep expertise in coroutines, Kotlin Multiplatform (KMP), and modern Kotlin 1.9+ patterns. |
| 4 | |
| 5 | ## Core Workflow |
| 6 | |
| 7 | 1. **Analyze architecture** - Identify platform targets, coroutine patterns, shared code strategy |
| 8 | 2. **Design models** - Create sealed classes, data classes, type hierarchies |
| 9 | 3. **Implement** - Write idiomatic Kotlin with coroutines, Flow, extension functions |
| 10 | - *Checkpoint:* Verify coroutine cancellation is handled (parent scope cancelled on teardown) and null safety is enforced before proceeding |
| 11 | 4. **Validate** - Run `detekt` and `ktlint`; verify coroutine cancellation handling and null safety |
| 12 | - *If detekt/ktlint fails:* Fix all reported issues and re-run both tools before proceeding to step 5 |
| 13 | 5. **Optimize** - Apply inline classes, sequence operations, compilation strategies |
| 14 | 6. **Test** - Write multiplatform tests with coroutine test support (`runTest`, Turbine) |
| 15 | |
| 16 | ## Reference Guide |
| 17 | |
| 18 | Load detailed guidance based on context: |
| 19 | |
| 20 | | Topic | Reference | Load When | |
| 21 | |-------|-----------|-----------| |
| 22 | | Coroutines & Flow | `references/coroutines-flow.md` | Async operations, structured concurrency, Flow API | |
| 23 | | Multiplatform | `references/multiplatform-kmp.md` | Shared code, expect/actual, platform setup | |
| 24 | | Android & Compose | `references/android-compose.md` | Jetpack Compose, ViewModel, Material3, navigation | |
| 25 | | Ktor Server | `references/ktor-server.md` | Routing, plugins, authentication, serialization | |
| 26 | | DSL & Idioms | `references/dsl-idioms.md` | Type-safe builders, scope functions, delegates | |
| 27 | |
| 28 | ## Key Patterns |
| 29 | |
| 30 | ### Sealed Classes for State Modeling |
| 31 | |
| 32 | ```kotlin |
| 33 | sealed class UiState<out T> { |
| 34 | data object Loading : UiState<Nothing>() |
| 35 | data class Success<T>(val data: T) : UiState<T>() |
| 36 | data class Error(val message: String, val cause: Throwable? = null) : UiState<Nothing>() |
| 37 | } |
| 38 | |
| 39 | // Consume exhaustively — compiler enforces all branches |
| 40 | fun render(state: UiState<User>) = when (state) { |
| 41 | is UiState.Loading -> showSpinner() |
| 42 | is UiState.Success -> showUser(state.data) |
| 43 | is UiState.Error -> showError(state.message) |
| 44 | } |
| 45 | ``` |
| 46 | |
| 47 | ### Coroutines & Flow |
| 48 | |
| 49 | ```kotlin |
| 50 | // Use structured concurrency — never GlobalScope |
| 51 | class UserRepository(private val api: UserApi, private val scope: CoroutineScope) { |
| 52 | |
| 53 | fun userUpdates(id: String): Flow<UiState<User>> = flow { |
| 54 | emit(UiState.Loading) |
| 55 | try { |
| 56 | emit(UiState.Success(api.fetchUser(id))) |
| 57 | } catch (e: IOException) { |
| 58 | emit(UiState.Error("Network error", e)) |
| 59 | } |
| 60 | }.flowOn(Dispatchers.IO) |
| 61 | |
| 62 | private val _user = MutableStateFlow<UiState<User>>(UiState.Loading) |
| 63 | val user: StateFlow<UiState<User>> = _user.asStateFlow() |
| 64 | } |
| 65 | |
| 66 | // Anti-pattern — blocks the calling thread; avoid in production |
| 67 | // runBlocking { api.fetchUser(id) } |
| 68 | ``` |
| 69 | |
| 70 | ### Null Safety |
| 71 | |
| 72 | ```kotlin |
| 73 | // Prefer safe calls and elvis operator |
| 74 | val displayName = user?.profile?.name ?: "Anonymous" |
| 75 | |
| 76 | // Use let to scope nullable operations |
| 77 | user?.email?.let { email -> sendNotification(email) } |
| 78 | |
| 79 | // !! only when the null case is a true contract violation and documented |
| 80 | val config = requireNotNull(System.getenv("APP_CONFIG")) { "APP_CONFIG must be set" } |
| 81 | ``` |
| 82 | |
| 83 | ### Scope Functions |
| 84 | |
| 85 | ```kotlin |
| 86 | // apply — configure an object, returns receiver |
| 87 | val request = HttpRequest().apply { |
| 88 | url = "https://api.example.com/users" |
| 89 | headers["Authorization"] = "Bearer $token" |
| 90 | } |
| 91 | |
| 92 | // let — transform nullable / introduce a local scope |
| 93 | val length = name?.let { it.trim().length } ?: 0 |
| 94 | |
| 95 | // also — side-effects without changing the chain |
| 96 | val user = createUser(form).also { logger.info("Created user ${it.id}") } |
| 97 | ``` |
| 98 | |
| 99 | ## Constraints |
| 100 | |
| 101 | ### MUST DO |
| 102 | - Use null safety (`?`, `?.`, `?:`, `!!` only when contract guarantees non-null) |
| 103 | - Prefer `sealed class` for state modeling |
| 104 | - Use `suspend` functions for async operations |
| 105 | - Leverage type inference but be explicit when needed |
| 106 | - Use `Flow` for reactive streams |
| 107 | - Apply scope functions appropriately (`let`, `run`, `apply`, `also`, `with`) |
| 108 | - Document public APIs with KDoc |
| 109 | - Use explicit API mode for libraries |
| 110 | - Run `detekt` and `ktlint` before committing |
| 111 | - Verify coroutine cancellation is handled (cancel parent scop |