Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Kotlinコンテキストパラメータをアプリ開発で使う夢を見る
Search
jj1uzh
June 30, 2026
100
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
6
4.9k
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
580
New Earth Scene 8
popppiees
4
2.7k
Git: the NoSQL Database
bkeepers
PRO
433
67k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Design in an AI World
tapps
1
350
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Embracing the Ebb and Flow
colly
88
5.2k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
500
Navigating Team Friction
lara
192
16k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
320
WCS-LA-2024
lcolladotor
0
870
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