Slide 28
Slide 28 text
KotlinConf 23 Keynote
Kotlin Handy to Know
ݣ౭ಁ۞
// GitHub API۽ ࢎਊ, ઑ, ࣗ ؘఠܳ ஂٙೞӝ ਤೠ ੋఠಕझ৬ ҳഅٜ
interface GithubOperations {
fun fetchUser(username: String): User
fun fetchOrganizations(user: User): List
fun fetchRepositories(user: User): List
}
class MockGithubOperations : GithubOperations { ... }
class KtorGithubOperations(httpClient: HttpClient) : GithubOperations { ... }
Kotlin Showcase
// Ҋର ೣࣻܳ ࢎਊ೧ࢲ GitHub ࠁܳ ઑ݀೧ࢲ ߈ജೠ
fun getGithubInfo(
username: String,
operations: GithubOperations,
assemble: (User, List, List) -> T): T {
val user = operations.fetchUser(username)
val organizations = operations.fetchOrganizations(user)
val repositories = operations.fetchRepositories(user)
return assemble(user, organizations, repositories)
}
// ೣࣻо زೞחؘ ਃೠ ઓҙ҅ܳ ׳ೞҊ, ۈܳ ా೧ GitHub ࠁܳ ਗೞח ߑधਵ۽ ઑ൩ೠ
getGithubInfo("jumpit", KtorGithubOperations(ktor())) { user, orgs, repos ->
// ױࣽೠ : ࢎਊ ܴҗ ઑ ࣻ, ୭Ӕ ழ ח ࣗ ࣻܳ ߈ജ
"User: ${user.name}, Orgs: ${orgs.size}, Repos: ${repos.count { it.hasRecentCommit }}"
}