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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
tomoEng11
April 21, 2026
Programming
230
0
Share
Explore CoroutineScope
tomoEng11
April 21, 2026
Other Decks in Programming
See All in Programming
Modding RubyKaigi for Myself
yui_knk
0
860
3Dシーンの圧縮
fadis
1
570
自動レビューエンジンの実装と運用 ~レビューのない世界へ~
kurukuru1999
2
310
Swiftのレキシカルスコープ管理
kntkymt
0
210
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
200
Inspired By RubyKaigi (EN)
atzzcokek
0
490
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
12
3.2k
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
210
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
450
プロパティの順序で型推論が壊れる!? TypeScript6.0の修正からContext-Sensitivityの仕組みを追う
bicstone
2
1.3k
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
230
誰も頼んでない機能を出荷した話
zekutax
0
160
Featured
See All Featured
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4k
Why Our Code Smells
bkeepers
PRO
340
58k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
70
39k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
130
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
600
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.1k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.3k
Typedesign – Prime Four
hannesfritz
42
3.1k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
180
GitHub's CSS Performance
jonrohan
1033
470k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
220
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設計めっちゃ勉強になる!!
ご清聴ありがとうございました