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
Kotlinコンテキストパラメータをアプリ開発で使う夢を見る
Search
jj1uzh
June 30, 2026
35
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Kotlinコンテキストパラメータをアプリ開発で使う夢を見る
Mobile Act Osaka 19
https://mobileact.connpass.com/event/393697/
jj1uzh
June 30, 2026
More Decks by jj1uzh
See All by jj1uzh
Hatena Engineer Seminar 37 jj1uzh
jj1uzh
0
120
Featured
See All Featured
Scaling GitHub
holman
464
140k
Marketing to machines
jonoalderson
1
5.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
The untapped power of vector embeddings
frankvandijk
2
1.8k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
330
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
870
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
2
250
Build your cross-platform service in a week with App Engine
jlugia
234
18k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
620
It's Worth the Effort
3n
188
29k
Writing Fast Ruby
sferik
630
63k
Transcript
Kotlinコンテキストパラメータ をアプリ開発で使う夢をみる id:jj1uzh 2026-06-17 MOBILE ACT OSAKA 19 1
自己紹介 • id:jj1uzh (miyachi) • 株式会社はてな ◦ 京都オフィス • アプリケーションエンジニア
◦ Web ◦ Androidは昨年から ◦ iOS入門中 2
Kotlin コンテキストパラメータとは • 2.2(experimental) → 2.4(stable) • 関数に暗黙的に引数を渡す • 渡す条件は型が一致
or 継承すること 3 context(c: SomeContext) fun foo(n: Int) { … c.somefunc(); … } context(someContext) { foo(123) }
Kotlin コンテキストパラメータとは • 一般的な使用例1: 引数の使い回し ◦ DSL 4 context(s: Session)
fun insertUser(u: User) { // DB操作の関数がSessionを s.insert(u.asEntity) // コンテキストから取り出す } context(newTransaction()) { insertUser(user) updateUserMetadata(user.metadata) commit() }
Kotlin コンテキストパラメータとは • 一般的な使用例1: 引数の使い回し ◦ コンテキスト伝搬 5 context(logger: Logger)
fun subHoge() { … logger.debug(…); … } context(logger: Logger) fun hoge() { … logger.info(…); subHoge(); … } // 伝搬 context(newLogger(label = “MyApp”)) { hoge() }
Kotlin コンテキストパラメータとは • 一般的な使用例2: 型クラス ◦ 継承に頼らず型の性質を定義 ◦ コンパニオンオブジェクトから自動で探す機能があれば… 6
fun interface Ordering<T> { fun cmp(a: T, b: T): Int } context(o: Ordering<T>) fun sort<T>(l: List<T>) { … o.cmp(l[i], l[j]) … }
7 アプリ開発での用途は?
関連する機能・実装 • DSL ◦ Function types with receivers • コンテキスト伝搬
◦ CoroutineContext,CompositionLocal,... • DI 8
vs Function types with receivers • A.(B) -> C ◦
ラムダ中の thisを指定 9 fun query(body: QueryBuilder.() -> Unit) class QueryBuilder { fun select(body: SelectBuilder.() -> Unit) } class SelectBuilder { fun columns(names: List<String>) } query { it.select { it.columns(…) } }
vs Function types with receivers • コンテキストパラメータで書くなら ◦ 各DSLメソッドはトップレベル関数に 10
fun query(body: context(QueryBuilder) () -> Unit): … context(q: QueryBuilder) fun select(body: context(SelectBuilder) () -> Unit) context(s: SelectBuilder) fun columns(names: List<String>) val q = query { select { columns(…) } } QueryBuilder
vs Function types with receivers • コンテキストを自然に増やせる 11 fun query(logger:
Logger, body: context(QueryBuilder) () -> Unit): … context(q: QueryBuilder, logger: Logger) fun select(body: context(SelectBuilder) () -> Unit) context(s: SelectBuilder, logger: Logger) fun columns(names: List<String>) val q = query { select { columns(…) } } QueryBuilder
vs Function types with receivers • 暗黙のthis vs 暗黙の引数 •
メリット ◦ コンテキストを自然に増やせる • デメリット ◦ DSL定義時の記述量は増える 12 QueryBuilder
vs 動的なコンテキスト伝搬 • 例えばRoomDatabase.withTransaction ◦ CoroutineContextから動的に取り出す ◦ 関数シグネチャに現れない 13 @Query(“...”)
fun createUser(u: UserEntity) // ??? database.withTransaction { createUser(user.asEntity) updateMetadata(user.asMetadataEntity) } QueryBuilder
vs 動的なコンテキスト伝搬 • コンテキストパラメータだったら ◦ コンパイル時に解決 14 @Query(“...”) context(session: Session)
// 見える化!!! fun createUser(u: UserEntity) fun withTx(body: context(Session) () -> Unit) { context(newTx()) { createUser(…) } } QueryBuilder
vs 動的なコンテキスト伝搬 • 例えばCompositionLocal ◦ 子孫のCompose関数から取り出せる値を定義 15 @Composable fun ThemeText(t:String){…
Theme.current …} @Composable fun AppScreen(){ ThemeText(“aaa”) } val Theme = compositionLocalOf { DefaultTheme() } CompositionLocalProvider(Theme provides LightTheme) { AppScreen() } QueryBuilder
vs 動的なコンテキスト伝搬 • コンテキストパラメータでも ◦ recomposeも動く & コンパイル時解決 16 QueryBuilder
@Composable context(theme: Theme) fun ThemeText(t:String){… theme …} @Composable context(theme: Theme) // ここも必要… fun AppScreen(){ ThemeText(“aaa”) } context(defaultTheme) { AppScreen() }
vs 動的なコンテキスト伝搬 • メリット ◦ コンパイル時に解決 ◦ 関数シグネチャの明示 • デメリット
◦ 記述量 ▪ 渡すのは暗黙だけど定義は毎回必要 • interfaceにしてミックスインしていけば集約できる 17 QueryBuilder
vs DI • classへのinjectionから→ 18 class DiaryRepository @Inject constructor(db: DB,
network: DiaryNetwork){ fun publish(diary: Diary) { db.deleteDraft(diary.id) network.publishDiary(diary.data) } }
vs DI • →関数ごとに依存する処理を明示 ◦ 最小権限、テストしやすさ 19 fun interface DeleteDraft
{ fun deleteDraft(id: Int) } context(d: DeleteDraft, p: PublishDiary) fun publish(diary: Diary) { d.deleteDraft(diary.id) p.publishDiary(diary.data) }
vs DI • Android的アーキテクチャでは? • 例えば、Composable関数の 依存する処理を明示できる可能性 20
21 vs DI • こういう構成が あったとして……
22 vs DI • 親のContextが子のContextの 積として自然に表現される
まとめ • コンテキストパラメータの活用できそうな 可能性を紹介した • 現状のしくみをすぐ置換るメリットは少ない ◦ 完全に置き換えるものでもない • コンパイル時解決、関数シグネチャは嬉しい
• 今後何らか使われていくかも …? 23 ご静聴ありがとうございました
参考資料(2026-06-16閲覧) • Language Guide / Context parameters https://kotlinlang.org/docs/context-parameters.html • Kotlin
2.2が切り拓く: コンテキストパラメータで書く関数型DSL と新しい依存管理のかたち https://speakerdeck.com/knih/context-parameters 24