Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Explore CoroutineScope
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
tomoEng11
April 21, 2026
Programming
250
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Explore CoroutineScope
tomoEng11
April 21, 2026
More Decks by tomoEng11
See All by tomoEng11
越境するプロダクトエンジニア
tomoeng11
0
9
Other Decks in Programming
See All in Programming
霧の中の代数的エフェクト
funnyycat
1
420
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
500
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
17
9.3k
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
140
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
150
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
180
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
150
共通化で考えるべきは、実装より公開する型だった
codeegg
0
270
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
350
yield再入門 #phpcon
o0h
PRO
0
710
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
130
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
2.9k
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
1
9.8k
Statistics for Hackers
jakevdp
799
230k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
660
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Automating Front-end Workflow
addyosmani
1370
210k
Product Roadmaps are Hard
iamctodd
55
12k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
260
Site-Speed That Sticks
csswizardry
13
1.3k
Everyday Curiosity
cassininazir
0
260
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
880
Transcript
Explore CoroutineScope Kotlinコルーチンにおける「型安全 × 柔軟性」の設計
About Me 名前: tomo 所属: 株式会社 Voicy プロダクトエンジニア X: @tomoEng11
好きな言語: Swift 最近Kotlinの勉強を始めました!
CoroutineScopeとは 非同期処理 (コルーチン )を親子関係でまとめて、ライフサイクルとキャンセルを管理する起点
CoroutineScopeの定義を詳しくみる 個人的な疑問 • CoroutineContextって何? • Jobを足してる? • context[Job]って何?
CoroutineContext コルーチンの「設定情報(プロパティ)の集合体」 クラス名 主な役割 CoroutineDispatcher スレッドの割り当て Job キャンセルや状態の追跡 CoroutineExceptionHandler 未キャッチ例外のハンドリング
CoroutineName コルーチンに名前を付与
CoroutineContextの構成
CoroutineContext.Element • Elementに対応するKeyが存在する • ElementもCoroutineContextになっている
だから Contextの操作がシンプルにできる
個人的な疑問 • ✅CoroutineContextって何? • ✅Jobを足してる? • context[Job]って何?
context[Job]の仕組み 本来はKeyを指定する必要があるのに、なんで型を指定しただけで取り出せるんだ?
CoroutineContextのgetメソッド 引数にKey<E>を要求している
Jobの定義を⾒てみる companion objectとして、Keyが定義されているから型名からアクセスできる
CoroutineContext
CoroutineContext minusKeyの引数がKey<*>になってるのはなんで?
minuskeyはなぜKey<*>なのか? Key<CoroutineContext.Element>でもよくない?
minuskeyとgetの違い メソッド 戻り値 Elementへの関心 get E? あり minusKey CoroutineContext なし
なぜKey<Element>ではダメか? Jobなどは、CoroutineContext.Elementのサブクラスだから Key<Job> != Key<CoroutineContext.Element> Keyのジェネリクスになった時点で、同一ではなくなる (invariant)
outにしてみたらどうか? 型とKeyの対応が崩れてしまう Key 期待する型 Key<Job> Job Key<CoroutineName> CoroutineName outにするとgetで正しい型が返せなくなる
まとめ • CoroutineContext は複数の要素を持つMap的構造 • Key はcompanion objectで定義されていて、型との対応を表現 • get
は型安全に取得 → Key<E> • minusKey は削除のみ → Key<*> • invariant を活かした設計で型安全を保証 KotlinのAPI設計めっちゃ勉強になる!!
ご清聴ありがとうございました