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
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.8k
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.4k
#Ubie 狂気の認知施策と選考設計
ntaro
14
14k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.2k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.7k
Kotlinでサーバサイドを始めよう!
ntaro
1
1.1k
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
3.1k
Kotlin Contracts #m3kt
ntaro
4
4.4k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
580
Featured
See All Featured
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
270
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Designing for Performance
lara
611
70k
WENDY [Excerpt]
tessaabrams
12
39k
Visualization
eitanlees
152
17k
Tell your own story through comics
letsgokoyo
1
1.1k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
45k
Ruling the World: When Life Gets Gamed
codingconduct
0
320
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
Leo the Paperboy
mayatellez
8
2.2k
How GitHub (no longer) Works
holman
316
150k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
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) コンパイルエラー