Kotlin

From G1Joshi/Agent-Skills · View interactive version

Kotlin is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Kotlin assistance covering coroutines, Flow, null safety, extension functions, sealed classes/interfaces, and Kotlin Multiplatform. Use when developing Android applications, server-side Kotlin services with Ktor/Spring, or sharing cross-platform business logic.

How to use this skill

Skill file

Kotlin

Modern Kotlin development with coroutines, null safety, and idiomatic patterns.

When to Use

Quick Start

data class User(
    val id: String,
    val name: String,
    val email: String,
    val createdAt: Instant = Clock.System.now()
)

suspend fun fetchUser(id: String): User? {
    return apiService.getUser(id)
}

Core Concepts

Kotlin Coroutines & Structured Concurrency

Lightweight cooperative multitasking with automated cancellation propagation:

import kotlinx.coroutines.*

suspend fun fetchUserProfile(userId: String): UserProfile = coroutineScope {
    // Run two network fetches in parallel
    val profileDeferred = async { api.getProfile(userId) }
    val ordersDeferred = async { api.getOrders(userId) }

    val profile = profileDeferred.await()
    val orders = ordersDeferred.await()
    profile.copy(orders = orders)
}

Kotlin Flow (Asynchronous Cold Streams)

Reactive streams with built-in backpressure and transformation operators:

import kotlinx.coroutines.flow.*

fun streamStockPrices(symbol: String): Flow<Double> = flow {
    while (true) {
        emit(api.getCurrentPrice(symbol))
        delay(1000)
    }
}.map { it * 1.05 } // Apply transformation
 .flowOn(Dispatchers.IO)

Extension Functions & Scope Functions

Extends existing classes without inheritance and scopes variable operations:

// Extension function on String
fun String.toSlug(): String = lowercase().replace(" ", "-").replace(Regex("[^a-z0-9-]"), "")

// Scope function (let, apply, run, also)
val user = User().apply {
    name = "Alex"
    role = "Lead"
}

Common Patterns

Parallel Coroutine Execution

Problem: Fetching multiple remote resources concurrently without blocking the main execution thread.

Solution:

// Suspend function
suspend fun fetchData(): List<Item> {
    return withContext(Dispatchers.IO) {
        api.fetchItems()
    }
}

// Parallel execution
suspend fun loadDashboard(): Dashboard {
    return coroutineScope {
        val user = async { fetchUser() }
        val orders = async { fetchOrders() }
        Dashboard(user.await(), orders.await())
    }
}

// Flow for streams
fun observeUsers(): Flow<List<User>> = flow {
    while (true) {
        emit(fetchUsers())
        delay(5000)
    }
}.flowOn(Dispatchers.IO)

Extension Functions

Problem: Adding domain-specific utility operations to standard or library types without inheritance.

Solution:

fun String.isValidEmail(): Boolean {
    return Regex("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}\$").matches(this)
}

fun <T> List<T>.secondOrNull(): T? = getOrNull(1)

inline fun <T> Result<T>.onSuccess(action: (T) -> Unit): Result<T> {
    if (this is Result.Success) action(data)
    return this
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| ----------------------- | -------------------- | ------------------------- |

| NullPointerException | Force unwrap on null | Use safe call ?. |

| CancellationException | Coroutine cancelled | Handle or propagate |

| IllegalStateException | Invalid state access | Check state before access |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.