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.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
570
Featured
See All Featured
Visualization
eitanlees
152
17k
It's Worth the Effort
3n
188
29k
Testing 201, or: Great Expectations
jmmastey
46
8.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
380
How STYLIGHT went responsive
nonsquared
100
6.2k
Embracing the Ebb and Flow
colly
88
5.1k
Making Projects Easy
brettharned
120
6.7k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
420
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
400
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
260
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) コンパイルエラー