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
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
The SEO identity crisis: Don't let AI make you average
varn
0
550
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Writing Fast Ruby
sferik
630
63k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
We Have a Design System, Now What?
morganepeng
55
8.3k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Odyssey Design
rkendrick25
PRO
2
800
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
67
58k
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) コンパイルエラー