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
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
440
Claspは野良GASの夢をみるか
takter00
0
160
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
140
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
350
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
2.2k
RTSPクライアントを自作してみた話
simotin13
0
450
LLM Plugin for Node-REDの利用方法と開発について
404background
0
160
Swiftのレキシカルスコープ管理
kntkymt
0
210
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
180
3Dシーンの圧縮
fadis
1
600
SPMマルチモジュールで テストカバレッジを取得する技法
yosshi4486
0
140
TypeScriptだけでAIエージェントを作る フロント・エージェント・インフラのフルスタック実践
har1101
6
1.3k
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
The agentic SEO stack - context over prompts
schlessera
0
790
Mind Mapping
helmedeiros
PRO
1
230
Navigating Team Friction
lara
192
16k
Marketing to machines
jonoalderson
1
5.3k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
560
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Agile that works and the tools we love
rasmusluckow
331
21k
Bash Introduction
62gerente
615
210k
What's in a price? How to price your products and services
michaelherold
247
13k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
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設計めっちゃ勉強になる!!
ご清聴ありがとうございました