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
230
0
Share
Explore CoroutineScope
tomoEng11
April 21, 2026
Other Decks in Programming
See All in Programming
RailsTokyo 2026#4: AI様があれば、 Hotwireの弱点は消えるか?
naofumi
5
910
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
960
Migrations : C'est une question d'hygiène !
vinceamstoutz
0
1.8k
Swiftのレキシカルスコープ管理
kntkymt
0
190
1人1案件のプロダクトエンジニア時代に、"プロセス監督"としてチャレンジしたこと
non0113
0
300
cloudnative conference 2026 flyle
azihsoyn
1
200
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
370
継続的な負荷検証を目指して
pyama86
3
1.5k
Agentic AI in the Frontend: Architectures with Open Standards @iJS London 2026
manfredsteyer
PRO
0
100
実践ハーネスエンジニアリング:ステアリングループを実例から読み解く / Practical Harness Engineering: Understanding Steering Loops Through Real-World Examples
nrslib
6
6.2k
「OSSがあるなら自作するな」は AI時代も正しいか ── Build vs Adopt の新しい判断基準
kumorn5s
7
2.9k
ReactとSvelteのその先、Ripple-TS / Beyond React and Svelte: Ripple-TS
ssssota
3
770
Featured
See All Featured
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
180
It's Worth the Effort
3n
188
29k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
190
Scaling GitHub
holman
464
140k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Unsuck your backbone
ammeep
672
58k
Building AI with AI
inesmontani
PRO
1
1k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
140
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
170
Color Theory Basics | Prateek | Gurzu
gurzu
0
310
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設計めっちゃ勉強になる!!
ご清聴ありがとうございました