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 Puzzlers
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Taro Nagasawa
April 01, 2018
1.5k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
変態文法とKotlin Puzzlers
Taro Nagasawa
April 01, 2018
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
1.7k
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
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
Featured
See All Featured
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
Mind Mapping
helmedeiros
PRO
1
250
Statistics for Hackers
jakevdp
799
230k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
730
What's in a price? How to price your products and services
michaelherold
247
13k
BBQ
matthewcrist
89
10k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
The Limits of Empathy - UXLibs8
cassininazir
1
360
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
Technical Leadership for Architectural Decision Making
baasie
3
410
Transcript
変態文法と Kotlin Puzzlers
長澤 太郎 • エムスリー株式会社 • 日本Kotlinユーザグループ代表
変態文法編
None
fun f(x: String) { x.fun String.() { println(toUpperCase()) }() }
fun f(x: String) { x.fun String.() { println(toUpperCase()) }() }
無名関数
fun f(x: String) { x.fun String.() { println(toUpperCase()) }() }
即時実行
fun f(x: String) { x.fun String.() { println(toUpperCase()) }() }
Stringの拡張関数を 無名関数として定義して 即実行している
None
val x = object: (()->Unit)->Unit by {}{}{}
val x = object: (()->Unit)->Unit by {}{}{} object: (()->Unit)->Unit by
{}{}{}
val x = object: (()->Unit)->Unit by {}{}{} object: (()->Unit)->Unit by
{}{}{} object: (Function0<Unit>)->Unit by {}{}{} 関数型() -> Unit は インタフェースFunction0<Unit>
val x = object: (()->Unit)->Unit by {}{}{} object: (()->Unit)->Unit by
{}{}{} object: (Function0<Unit>)->Unit by {}{}{} object: Function1<Function0<Unit>, Unit> by {}{}{}
val x = object: (()->Unit)->Unit by {}{}{} object: (()->Unit)->Unit by
{}{}{} object: (Function0<Unit>)->Unit by {}{}{} object: Function1<Function0<Unit>, Unit> by {}{}{} インタフェースのオブジェクト式(=無名クラス化)
val x = object: (()->Unit)->Unit by {}{}{} object: (()->Unit)->Unit by
{}{}{} object: (Function0<Unit>)->Unit by {}{}{} object: Function1<Function0<Unit>, Unit> by {}{}{} 何もしない関数であるラムダ式{}にbyにより委譲
val x = object: (()->Unit)->Unit by {}{}{} object: (()->Unit)->Unit by
{}{}{} object: (Function0<Unit>)->Unit by {}{}{} object: Function1<Function0<Unit>, Unit> by {}{}{} 無名クラスの本体ブロック
val x = object: (()->Unit)->Unit by {}{}{} object: (()->Unit)->Unit by
{}{}{} object: (Function0<Unit>)->Unit by {}{}{} object: Function1<Function0<Unit>, Unit> by {}{}{} 無名クラス(1つの引数を取る関数)の即時実行で、 ラムダ式を渡している
None
fun main(args: Array<String>) { x.(y())() }
fun main(args: Array<String>) { x.(y())() } xのメソッド呼び出し or 拡張関数呼び出し
fun main(args: Array<String>) { x.(y())() } y()呼び出しによって、xの拡張関数を得る
val x = 0 fun y(): Int.()->Unit = {} fun
main(args: Array<String>) { x.(y())() }
object x fun y(): x.()->Unit = {} fun main(args: Array<String>)
{ x.(y())() }
None
fun Nothing.foo() {}
fun Nothing.foo() {} listOf<Nothing>().first().foo()
fun Nothing.foo() {} listOf<Nothing>().first().foo() null?.foo()
fun Nothing.foo() {} listOf<Nothing>().first().foo() null?.foo() return.foo()
fun Nothing.foo() {} listOf<Nothing>().first().foo() null?.foo() return.foo() (throw Exception()).foo()
nullがNothingであるおかげで... nullがあらゆるnull許容型変数に代入可能 val a: String? = null val b: Int?
= null val c: List<Foo>? = null returnやthrowがNothingであるおかげで... Nullable -> NotNull 変換が捗る val x: Foo = xx ?: return val y: Bar = yy ?: throw Exception()
Kotlin Puzzlers編
fun Array<String>.main() = "Hello, world" ------------------------------------------- 1) 「Hello, world」と出力される 2)
何も起こらない 3) コンパイルエラー 4) 実行時例外
fun Array<String>.main() = "Hello, world" ------------------------------------------- 1) 「Hello, world」と出力される 2)
何も起こらない 3) コンパイルエラー 4) 実行時例外
fun main(args: Array<String>) { { return@main } print("A") run {
return@main } print("B") } -------------------------------------------- 1) 何も起こらない 2) 「A」と出力される 3) 「AB」と出力される 4) コンパイルエラー
fun main(args: Array<String>) { { return@main } print("A") run {
return@main } print("B") } -------------------------------------------- 1) 何も起こらない 2) 「A」と出力される 3) 「AB」と出力される 4) コンパイルエラー
fun main(args: Array<String>) { val f: String.() -> Unit =
::print "A".f() f("B") } --------------------------------------------- 1) 何も起こらない 2) 「A」と出力される 3) 「AB」と出力される 4) コンパイルエラー
fun main(args: Array<String>) { val f: String.() -> Unit =
::print "A".f() f("B") } --------------------------------------------- 1) 何も起こらない 2) 「A」と出力される 3) 「AB」と出力される 4) コンパイルエラー
fun main(args: Array<String>) { var a: Int? = -128 val
b: Int? = -129 print(a === -128); print(", ") print(a?.minus(1) === -129); print(", ") print(b === -129) } --------------------------------------------- 1) 「true, true, true」と出力される 2) 「true, false, true」と出力される 3) 「true, true, false」と出力される 4) コンパイルエラー
fun main(args: Array<String>) { var a: Int? = -128 val
b: Int? = -129 print(a === -128); print(", ") print(a?.minus(1) === -129); print(", ") print(b === -129) } --------------------------------------------- 1) 「true, true, true」と出力される 2) 「true, false, true」と出力される 3) 「true, true, false」と出力される 4) コンパイルエラー
fun main(args: Array<String>) { val (x) = object { operator
fun component0() = "Zero" operator fun component1() = "One" override fun toString() = "Unknown" } println((x)) } --------------------------------------------- 1) 「Zero」と出力される 2) 「One」と出力される 3) 「Unknown」と出力される 4) コンパイルエラー
fun main(args: Array<String>) { val (x) = object { operator
fun component0() = "Zero" operator fun component1() = "One" override fun toString() = "Unknown" } println((x)) } --------------------------------------------- 1) 「Zero」と出力される 2) 「One」と出力される 3) 「Unknown」と出力される 4) コンパイルエラー