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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Taro Nagasawa
April 07, 2017
Programming
1
790
Kotlinの関数参照 #Kotlin_Sansan
Qiita記事をそのままスライド化。
http://qiita.com/ngsw_taro/items/f2f70296828ee75491a2
Taro Nagasawa
April 07, 2017
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
1.2k
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.3k
#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
2.9k
Kotlin Contracts #m3kt
ntaro
4
4.3k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
540
Other Decks in Programming
See All in Programming
AI時代の認知負荷との向き合い方
optfit
0
170
並行開発のためのコードレビュー
miyukiw
0
1.2k
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
750
「ブロックテーマでは再現できない」は本当か?
inc2734
0
1k
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
210
Best-Practices-for-Cortex-Analyst-and-AI-Agent
ryotaroikeda
1
110
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
330
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
Raku Raku Notion 20260128
hareyakayuruyaka
0
360
AIによる高速開発をどう制御するか? ガードレール設置で開発速度と品質を両立させたチームの事例
tonkotsuboy_com
7
2.4k
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
180
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
380
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
698
190k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
54
How to build a perfect <img>
jonoalderson
1
4.9k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
エンジニアに許された特別な時間の終わり
watany
106
230k
Test your architecture with Archunit
thirion
1
2.2k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
460
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.1k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
150
Everyday Curiosity
cassininazir
0
130
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
110
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) • プロパティも関数オブジェクトになれる