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
Double Context Extension (Kotlin) #Retty_tech_cafe
Search
Taro Nagasawa
December 09, 2016
Programming
0
310
Double Context Extension (Kotlin) #Retty_tech_cafe
Retty Tech Cafe #8 (
https://retty.connpass.com/event/45628/)で発表した資料です
。
Taro Nagasawa
December 09, 2016
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
740
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.2k
#Ubie 狂気の認知施策と選考設計
ntaro
13
13k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.5k
Kotlinでサーバサイドを始めよう!
ntaro
1
990
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.8k
Kotlin Contracts #m3kt
ntaro
4
4.1k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
510
Other Decks in Programming
See All in Programming
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
670
0から始めるモジュラーモノリス-クリーンなモノリスを目指して
sushi0120
1
280
Introduction to Git & GitHub
latte72
0
110
バイブコーディング × 設計思考
nogu66
0
120
生成AI、実際どう? - ニーリーの場合
nealle
0
110
CEDEC2025 長期運営ゲームをあと10年続けるための0から始める自動テスト ~4000項目を50%自動化し、月1→毎日実行にした3年間~
akatsukigames_tech
0
140
バイブコーディングの正体——AIエージェントはソフトウェア開発を変えるか?
stakaya
5
940
iOS開発スターターキットの作り方
akidon0000
0
250
AI時代のドメイン駆動設計-DDD実践におけるAI活用のあり方 / ddd-in-ai-era
minodriven
19
7.4k
GitHub Copilotの全体像と活用のヒント AI駆動開発の最初の一歩
74th
7
2.8k
Flutter로 Gemini와 MCP를 활용한 Agentic App 만들기 - 박제창 2025 I/O Extended Seoul
itsmedreamwalker
0
140
AHC051解法紹介
eijirou
0
560
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
How to Think Like a Performance Engineer
csswizardry
25
1.8k
Fireside Chat
paigeccino
39
3.6k
Building Adaptive Systems
keathley
43
2.7k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.8k
Visualization
eitanlees
146
16k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.4k
How to Ace a Technical Interview
jacobian
279
23k
Scaling GitHub
holman
462
140k
Transcript
Double Context Extension Retty Tech Cafe #8 2016-12-09 長澤 太郎
自己紹介 • 長澤 太郎 たろーって呼んでね ◦ @ngsw_taro • エンジニア@エムスリー株式会社 ◦
Android, Kotlin, Java, Spring, Railsなど • Kotlinエバンジェリスト • ディズニーが好き
Kotlinいいよね! • 型推論 • ラムダ式 • NULL安全 • Extension •
委譲プロパティ • 分解宣言 • コルーチン • 型エイリアス
Kotlinいいよね! • 型推論 • ラムダ式 • NULL安全 • Extension •
委譲プロパティ • 分解宣言 • コルーチン • 型エイリアス
Extension(拡張) • 拡張関数 • 拡張プロパティ fun String.hello() { println("Hello, $this")
} "world".hello() val String.initial: Char? get()=firstOrNull()?.toUpperCase() "world".initial
拡張関数じゃ物足りない問題
こんな関数呼び出しをしたい! class MyActivity: FragmentActivity { ... fun showMessage() { //
myDialog.show(supportFragmentManager, "tag") myDialog.show("tag") } }
こんな関数呼び出しをしたい! class MyActivity: FragmentActivity { ... fun showMessage() { //
myDialog.show(supportFragmentManager, "tag") myDialog.show("tag") } } DialogFragmentに拡張関数showを生やせば? いや、プロパティsupportFragmentManagerが必要だ... →FragmentManagerに依存
クラスの中に拡張関数を定義してみる class MyActivity: FragmentActivity { ... fun FragmentDialog.show(tag: String?) {
show(supportFragmentManager, tag) } fun showMessage() { myDialog.show("tag") } }
クラスの中に拡張関数を定義してみる class MyActivity: FragmentActivity { ... fun FragmentDialog.show(tag: String?) {
show(supportFragmentManager, tag) } fun showMessage() { myDialog.show("tag") } } イケる!?
再利用ができない問題... class MyActivity: FragmentActivity { ... fun FragmentDialog.show(tag: String?) {
show(supportFragmentManager, tag) } } class YourActivity: FragmentActivity { ... fun FragmentDialog.show(tag: String?) { show(supportFragmentManager, tag) } }
再利用ができない問題... class MyActivity: FragmentActivity { ... fun FragmentDialog.show(tag: String?) {
show(supportFragmentManager, tag) } } class YourActivity: FragmentActivity { ... fun FragmentDialog.show(tag: String?) { show(supportFragmentManager, tag) } } 同じコードが重複する
どうするか?
if (ちょっとした実験とか趣味のコード) { } else { }
if (ちょっとした実験とか趣味のコード) { recommend(DoubleContextExtension) } else { }
if (ちょっとした実験とか趣味のコード) { recommend(DoubleContextExtension) } else { recommend(Extension至上主義を捨てよ) }
Double Context Extension • 2つのコンテキストを持った拡張関数 • 命名 by 私 •
見た目: 型Aの定義中で、型Aに依存しながらも型Bの拡張関 数を生やすことができる class MyActivity: FragmentActivity { ... fun showMessage() { myDialog.show("tag") } }
Double Context Extension • 実際のコード: 「B型の拡張関数」を返す「A型の拡張プロパ ティ」を定義する // どこか適当なファイル val
FragmentActivity.show: DialogFramgnet.(String?)->Unit get() { tag -> show(supportFragmentManager, tag) }
どう解釈されるか val FragmentActivity.show: DialogFramgnet.(String?)->Unit get() { tag -> show(supportFragmentManager, tag)
} class MyActivity: FragmentActivity { fun showMessage() { myDialog.show("tag") } ... }
どう解釈されるか val FragmentActivity.show: DialogFramgnet.(String?)->Unit get() { tag -> show(supportFragmentManager, tag)
} class MyActivity: FragmentActivity { fun showMessage() { myDialog.show("tag") } ... } FragmentActivityの 拡張関数showの呼び出し
どう解釈されるか val FragmentActivity.show: DialogFramgnet.(String?)->Unit get() { tag -> show(supportFragmentManager, tag)
} class MyActivity: FragmentActivity { fun showMessage() { myDialog.(this.show)("tag") } ... } こう書いても同じ
どう解釈されるか val FragmentActivity.show: DialogFramgnet.(String?)->Unit get() { tag -> show(supportFragmentManager, tag)
} class MyActivity: FragmentActivity { fun showMessage() { myDialog.show("tag") } ... } 返って来た拡張関数オブジェクトを myDialogをレシーバに、 ”tag”を引数にして呼び出す
まとめ • 拡張関数って便利だよね • でも限界がある • Double Context Extensionという提案
まとめ • 拡張関数って便利だよね • でも限界がある • Double Context Extensionという提案 •
変なことしないで素直なコードを心がけましょう