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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
tomoEng11
April 21, 2026
Programming
240
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Explore CoroutineScope
tomoEng11
April 21, 2026
Other Decks in Programming
See All in Programming
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
130
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
250
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
180
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
140
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
100
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
160
New "Type" system on PicoRuby
pocke
1
930
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
230
Agentic UI
manfredsteyer
PRO
0
160
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
6.2k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
140
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
How to build a perfect <img>
jonoalderson
1
5.7k
Optimising Largest Contentful Paint
csswizardry
37
3.7k
Building AI with AI
inesmontani
PRO
1
1.1k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
140
How to Talk to Developers About Accessibility
jct
2
230
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Believing is Seeing
oripsolob
1
150
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
160
The Language of Interfaces
destraynor
162
27k
A Tale of Four Properties
chriscoyier
163
24k
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設計めっちゃ勉強になる!!
ご清聴ありがとうございました