Slide 1

Slide 1 text

Kotlin ΢ΥʔΫεϧʔ 2014-07-04 ୈ2ճ͔Θ͍͍Kotlinษڧձ ! ೔ຊKotlinϢʔβάϧʔϓ
 ௕ᖒ ଠ࿠

Slide 2

Slide 2 text

Kotlinͱ͸ • ϓϩάϥϛϯάݴޠ • JVMͰಈ͘ • JetBrains͕த৺ʹ։ൃ͍ͯ͠ΔOSS • 2011೥ʹൃද • ݱόʔδϣϯ͸0.7

Slide 3

Slide 3 text

ͳͥ,PUMJOͳͷ͔ • δϟό͸ͪΐͬͱ… • Scalaͬͯػೳ͕ଟͯ͘೉͍͠ • Groovy͸ಈతͰ೉͍͠ • ͪΐ͏Ͳ͍͍ͱ͜Ζ͕ཉ͍͠ʂ

Slide 4

Slide 4 text

Kotlin͕໨ࢦ͢ͱ͜Ζ • γϯϓϧ • ҆શ • εϐʔυ ͬ͘͟Γݴ͏ͱ…

Slide 5

Slide 5 text

Kotlinͷಛ௃ • ؆ܿ • ҆શ • JVMͰಈ͘ • JavaScriptʹ΋ม׵Ͱ͖Δ • ੩తܕ෇͚ • ΦϒδΣΫτࢦ޲ • ؔ਺ܕϓϩάϥϛϯά

Slide 6

Slide 6 text

Hello World package hello fun main(args : Array) { println("Hello, world!") }

Slide 7

Slide 7 text

Hello World package hello fun main(args : Array) { println("Hello, world!") } ύοέʔδ௚Լʹ ؔ਺͕ஔ͚Δ ΤϯτϦϙΠϯτ͸ mainؔ਺

Slide 8

Slide 8 text

Hello World package hello fun main(args : Array) { println("Hello, world!") } ม਺ͷܕ͕ޙஔ ഑ྻ͕ δΣωϦΫε

Slide 9

Slide 9 text

Hello World package hello fun main(args : Array) { println("Hello, world!") } ηϛίϩϯ ෆཁ

Slide 10

Slide 10 text

Ϋϥε class Greeter ! val greeter = Greeter()

Slide 11

Slide 11 text

Ϋϥε class Greeter ! val greeter = Greeter() ࠷খͷ Ϋϥεఆٛ

Slide 12

Slide 12 text

Ϋϥε class Greeter ! val greeter = Greeter() ΠϯελϯεԽ newෆཁ ܕࢦఆෆཁ: ܕਪ࿦

Slide 13

Slide 13 text

Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello, $name") } }

Slide 14

Slide 14 text

Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello, $name") } } ϓϩύςΟ val g = Greeter(“hoge”) val name = g.name

Slide 15

Slide 15 text

Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello, $name") } } ϝιου ࣜ(ม਺)ΛຒΊࠐΊΔ

Slide 16

Slide 16 text

ϓϩύςΟ class User(name: String) { var name: String = name } ! val user = User("Taro") user.name / / => "Taro"

Slide 17

Slide 17 text

ϓϩύςΟ class User(name: String) { var name: String = name } ! val user = User("Taro") user.name / / => "Taro" ίϯετϥΫλ ϓϩύςΟ

Slide 18

Slide 18 text

ϓϩύςΟ class User(name: String) { var name: String = name } ! val user = User("Taro") user.name / / => "Taro" ϑΟʔϧυʹ௚઀ ΞΫηεͯ͠Δ෩

Slide 19

Slide 19 text

ΞΫηα class User(name: String) { var name: String = name set(name: String) {$name = name } get(): String = $name }

Slide 20

Slide 20 text

ΞΫηα class User(name: String) { var name: String = name set(name: String) {$name = name } get(): String = $name } Setter Getter Backing Field

Slide 21

Slide 21 text

Delegated Property class User(name: String) { val name: String by Decorator(name) } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "<<$str>>" }

Slide 22

Slide 22 text

Delegated Property class User(name: String) { val name: String by Decorator(name) } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "<<$str>>" } User#name΁ͷ ΞΫηε͕ҕৡ͞ΕΔ

Slide 23

Slide 23 text

Ԡ༻ྫ class MainActivity: Activity() { val submitButton: Button by injector() ! fun onCreate(savedInstanceState: Bundle?) { submitButton.setOnClickListener(::submit) } }

Slide 24

Slide 24 text

ܕ҆શ val a: Array = array(1, 2, 3) val b: Array = a / / NG val c: Array = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG

Slide 25

Slide 25 text

ܕ҆શ val a: Array = array(1, 2, 3) val b: Array = a / / NG val c: Array = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG Array͸σϑΥϧτͰ ෆม(invariant) มҐࢦఆ͕Մೳ ͜͜Ͱ͸ڞม(covariant)

Slide 26

Slide 26 text

ܕ҆શ val a: Array = array(1, 2, 3) val b: Array = a / / Error val c: Array = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG ܕͷෆ੔߹Λ ๷͍Ͱ͘ΕΔ

Slide 27

Slide 27 text

એݴՕॴ෼ࢄ val a: List = listOf(1, 2, 3) val b: List = a / / OK ———————————————————————— class List …

Slide 28

Slide 28 text

એݴՕॴ෼ࢄ val a: List = listOf(1, 2, 3) val b: List = a ———————————————————————— class List … Ϋϥεͷએݴ࣌ʹ มҐࢦఆͰ͖Δ

Slide 29

Slide 29 text

ؔ਺͕first-class val add = { (a: Int, b: Int) -> a + b } ! add(3, 4) / / => 7

Slide 30

Slide 30 text

ؔ਺͕first-class list.filter({ (e: Int) -> e % 2 != 0 })

Slide 31

Slide 31 text

ؔ਺͕first-class list filter { it % 2 != 0 }

Slide 32

Slide 32 text

Single Abstract Method / / Java static void invoke(Runnable r) { r.run(); } ! / / Kotlin Sample.invoke { hoge() } Runnableͷ୅ΘΓʹ ()->Unitͳؔ਺Λࢦఆ

Slide 33

Slide 33 text

ΠϯϥΠϯؔ਺ inline fun invoke(f: ()->Unit) { f() }

Slide 34

Slide 34 text

ΠϯϥΠϯؔ਺ inline fun invoke(f: ()->Unit) { f() } inlineΞϊςʔγϣϯ: ΠϯϥΠϯల։͞ΕΔ ؔ਺ͷܕ (Ҿ਺ͷܕ)->ฦ஋ͷܕ

Slide 35

Slide 35 text

֦ுؔ਺ fun String.hello() { println("Hello, $this") } ! "World".hello()

Slide 36

Slide 36 text

֦ுؔ਺ + ؔ਺ΦϒδΣΫτ fun String.hello() { println("Hello, $this") } ! "World".hello()

Slide 37

Slide 37 text

TCO tailRecursive fun sum(ints: List, sum: Int = 0): Int = if(ints.isEmpty()) sum else sum(ints.tail, sum + ints.first())

Slide 38

Slide 38 text

TCO tailRecursive fun sum(ints: List, sum: Int = 0): Int = if(ints.isEmpty()) sum else sum(ints.tail, sum + ints.first()) tailRecursive Ξϊςʔγϣϯ

Slide 39

Slide 39 text

null҆શ val a: Int = null / / OK val b: Int? = null / / NG b.toString() / / NG if(b != null) b.toString() / / OK b?.toString() / / OK

Slide 40

Slide 40 text

null҆શ val a: Int = null / / OK val b: Int? = null / / NG b.toString() / / NG if(b != null) b.toString() / / OK b?.toString() / / OK if (b != null) b.toString() else null

Slide 41

Slide 41 text

null҆શ fun toInt(s: String): Int? fun square(n: Int): Int จࣈྻΛtoInt()Ͱ਺஋ʹม׵ ͦͷ਺Λsquare()Ͱೋ৐͍ͨ͠

Slide 42

Slide 42 text

ෳࡶ… if(s != null) { val i = toInt(s) if(i != null) square(i) else null } else { null }

Slide 43

Slide 43 text

ศརͳؔ਺Λಋೖ fun T.bind(f: ((T) -> R)?): R = f?.invoke(this)

Slide 44

Slide 44 text

bindΛ࢖͏ͱΩϨΠʹͳΔ s?.bind(::toInt)?.bind(::square)

Slide 45

Slide 45 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠