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の関数参照 #Kotlin_Sansan
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Taro Nagasawa
April 07, 2017
Programming
790
1
Share
Kotlinの関数参照 #Kotlin_Sansan
Qiita記事をそのままスライド化。
http://qiita.com/ngsw_taro/items/f2f70296828ee75491a2
Taro Nagasawa
April 07, 2017
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
1.5k
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.4k
#Ubie 狂気の認知施策と選考設計
ntaro
13
14k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.2k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.6k
Kotlinでサーバサイドを始めよう!
ntaro
1
1k
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
3k
Kotlin Contracts #m3kt
ntaro
4
4.4k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
560
Other Decks in Programming
See All in Programming
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
720
書き換えて学ぶTemporal #fukts
pirosikick
2
330
Making the RBS Parser Faster
soutaro
0
640
KMP × Kotlin 2.3 - How Android Got Slower While iOS Builds Improved by 47%
rio432
0
100
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
200
個人的に嬉しかったpnpmの新機能・3選
matsuo_atsushi
0
120
AI-DLC Deep Dive
yuukiyo
9
5.3k
PHPでローカル環境用のSSL/TLS証明書を発行することはできるのか? #phpconkagawa
akase244
0
300
ついに来た!本格的なマルチクラウド時代の Google Cloud
maroon1st
0
340
How Swift's Type System Guides AI Agents
koher
0
320
AWSコミュニティ活動は顧客のクラウド推進に効くのか / Do AWS community activities help customers adopt the cloud?
seike460
PRO
0
160
【26新卒研修資料】TDD実装演習
dip_tech
PRO
0
160
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
110
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
820
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.8k
Making the Leap to Tech Lead
cromwellryan
135
9.8k
Embracing the Ebb and Flow
colly
88
5k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
190
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Google's AI Overviews - The New Search
badams
0
990
Agile that works and the tools we love
rasmusluckow
331
21k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
Crafting Experiences
bethany
1
130
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
360
Transcript
Kotlinの関数参照 2017-04-04 Kotlin勉強会@Sansan 長澤 太郎
自己紹介 • たろう @ngsw_taro • エムスリー株式会社のAndroidエンジニア • Kotlinエバンジェリスト(自称) • 著書「Kotlinスタートブック」
もくじ 1. 関数参照 2. レシーバが未定のメソッド参照 3. レシーバが確定のメソッド参照 4. プロパティも関数
1. 関数参照
関数オブジェクト • Kotlinでは定義済みの関数のオブジェクトを得ること ができる • 関数オブジェクトを変数に代入したり、別の関数の引 数に渡したり、持ち回して便利に使える。 • 必ずしもオブジェクトではない(ヒント: インライン関
数)
コード例 // 普通の関数 fun succ(n: Int): Int = n +
1 // 関数オブジェクトを取得し、変数に代入 val myFunc: (Int)->Int = ::succ // ^^^^^^^^^^ 関数型 // メソッド呼び出しによる元の関数の計算を実行 myFunc.invoke(5) //=> 6 // 演算子オーバロードによる関数らしい記述 myFunc(7) //=> 8
応用例 // 高階関数の引数に「関数参照」を渡す listOf(1, 2, 3).map(::succ) //=> [2, 3, 4]
// ラムダ式を使えば同じ listOf(1, 2, 3).map { succ(it) } //=> [2, 3, 4] • ラムダ式は、文を手続き的に記述していく • 関数参照は、宣言的な記述になる
2. レシーバが未定のメソッド参照
メソッド参照 • メソッドのオブジェクトも取得できる • この場合は、レシーバが未定 // 「型::メソッド名」という書式で取得 val foo: String.()->String
= String::reversed // ^^^^^^^^^^^^^^^^^ 関数型 // メソッドっぽく呼び出せる "Kotlin".foo() //=> niltoK
拡張関数もOK fun String.bold(): String = "**$this**" val baz: String.()->String =
String::bold "Sansan".baz() //=> **Sansan**
普通の関数型にもなるよ • A.(B)->Cという型は、(A, B)->Cと同じっぽい fun String.bold(): String = "**$this**" //
型どっちでもOK val aaa: String.()->String = String::bold val bbb: (String)->String = aaa // 呼び出し方どっちでもOK "hoge".aaa() //=> **hoge** aaa("fuga") //=> **fuga**
高階関数で効果を発揮 "Hello".let { it.bold() } "Hello".let(String::bold) もしString.()->Stringが(String)->Stringと みなせなかったら、2行目の書き方がNGです。
3. レシーバが確定のメソッド参照
Bound Function Reference • bound = 束縛された → レシーバが確定してるメソッド参照 •
Kotlin 1.1から使用できる
コード例 // 「オブジェクト::メソッド名」の書式で取得 val succ: (Int)->Int = 1::plus succ(5) //=>
6 userRepository.findById(123L) ?.let { user -> user.copy(blocked = true) } ?.let(userRepository::update)
4. プロパティも関数
プロパティも関数として扱える val prop: KProperty1<String, Int> = String::length prop.get("Taro") //=> 4
val func: (String) -> Int = prop func("Taro") //=> 4
高階関数で効果を発揮 listOf("foo", "wa").map { it.length } // => [3, 2]
listOf("foo", "wa").map(String::length) // => [3, 2] もしKProperty1<String, Int>が (String)->Stringとみなせなかったら、2行目の書き 方がNGです。
まとめ • 関数オブジェクトを使うと宣言的な記述ができる • ::hoge: 関数参照 • Foo::bar: メソッド参照 •
foo::bar: レシーバ固定メソッド参照 (Bound Function Reference) • プロパティも関数オブジェクトになれる