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
スマートグラスで並列バイブコーディング
hyshu
0
140
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
Oxlintのカスタムルールの現況
syumai
6
1.1k
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
130
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
620
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
8
4.7k
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.7k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
130
Creating Composable Callables in Contemporary C++
rollbear
0
130
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
2k
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
170
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
190
Featured
See All Featured
Google's AI Overviews - The New Search
badams
0
1k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
320
The untapped power of vector embeddings
frankvandijk
2
1.8k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
330
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
370
Leo the Paperboy
mayatellez
7
1.8k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Designing for Timeless Needs
cassininazir
1
250
How to train your dragon (web standard)
notwaldorf
97
6.7k
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設計めっちゃ勉強になる!!
ご清聴ありがとうございました