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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
jj1uzh
June 30, 2026
110
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
5k
Featured
See All Featured
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
The Cult of Friendly URLs
andyhume
79
7.1k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k
Why Our Code Smells
bkeepers
PRO
340
58k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
3k
Utilizing Notion as your number one productivity tool
mfonobong
4
630
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.6k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
820
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
360
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