Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
変態文法と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
590
Featured
See All Featured
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
310
Building Adaptive Systems
keathley
44
3.2k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
450
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
We Are The Robots
honzajavorek
0
380
Statistics for Hackers
jakevdp
799
230k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
860
Chasing Engaging Ingredients in Design
codingconduct
0
340
Amusing Abliteration
ianozsvald
1
330
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
What's in a price? How to price your products and services
michaelherold
247
13k
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) コンパイルエラー