Slide 1

Slide 1 text

Googleެࣜαϙʔτʂ Kotlinೖ໳ @magie_pooh αϙʔλʔζKotlinษڧձ #3

Slide 2

Slide 2 text

ࣗݾ঺հ ౻ాୖຏ @magie_pooh @magiepooh

Slide 3

Slide 3 text

FOLIO γϦʔζAϥ΢ϯυͰ18ԯԁͷࢿۚௐୡΛ࣮ࢪ͠·͠ ͨɻ೔ຊॳͷςʔϚ౤ࢿܕΦϯϥΠϯূ݊Λͭ͘Γ·͢ɻ

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

What’s Kotlin?

Slide 6

Slide 6 text

Kotlin • Jet Brains ͕࡞ͬͨJVMݴޠ • ੩తܕ෇͚ͷΦϒδΣΫτࢦ޲ݴޠ • GoogleIOͰAndroidͷ։ൃݴޠͱͯ͠ެࣜαϙʔτ͕ൃද • 2012೥ࠒ M1ϦϦʔε • 2016೥ 1.0 ϦϦʔε • Support JS(Kotlin/JS) • LLVM(Kotlin/Native)

Slide 7

Slide 7 text

Kotlin • Lambdas • Nullable(NonNull) • High-Order Functions(Stream) • Extension Functions

Slide 8

Slide 8 text

Kotlin • Lambdas -> Retrolambda • Nullable(NonNull) -> Optional • High-Order Functions(Stream) -> StremeAPI, RxJava • Extension Functions

Slide 9

Slide 9 text

Kotlin • Lambdas -> Retrolambda • Nullable(NonNull) -> Optional • High-Order Functions(Stream) -> StremeAPI, RxJava • Extension Functions

Slide 10

Slide 10 text

ࠓ೔ͷΰʔϧ • είʔϓؔ਺ͷཧղ • apply • let • run • with

Slide 11

Slide 11 text

ม਺

Slide 12

Slide 12 text

val num : Int = 1
 var num2 : Int = 2
 
 num = 3 // error
 num2 = 3 ม਺ͷએݴ

Slide 13

Slide 13 text

val num = 1
 var num2 = 2
 
 num = 3 // error
 num2 = 3 ม਺ͷએݴ

Slide 14

Slide 14 text

Null҆શ var a: String = "abc"
 a = null // => ίϯύΠϧΤϥʔ
 
 var b: String? = "abc"
 b = null // => OK

Slide 15

Slide 15 text

Null҆શʢݺͼग़͠ʣ var b: String? = "abc"
 val l : Int = b.length // => ίϯύΠϧΤϥʔ
 
 var b: String? = "abc"
 val l : Int = if(b != null) {
 b.length
 } else {
 -1
 } 
 val l : Int? = b?.length val l : Int = b?.length ?: -1

Slide 16

Slide 16 text

Null҆શʢΤϧϏεԋࢉࢠʣ var b: String? = "abc"
 val l : Int = if(b != null) b.length else -1 val l : Int = b?.length ?: -1

Slide 17

Slide 17 text

ؔ਺

Slide 18

Slide 18 text

ؔ਺ͷجຊ fun add(x: Int, y: Int): Int {
 return x + y
 }

Slide 19

Slide 19 text

ؔ਺ͷجຊ fun add(x: Int, y: Int): Int = x + y

Slide 20

Slide 20 text

ؔ਺ͷجຊ fun add(x: Int, y: Int) = x + y

Slide 21

Slide 21 text

ߴ֊ؔ਺

Slide 22

Slide 22 text

ߴ֊ؔ਺ // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ
 fun lock(lock: Lock, body: () -> T): T {
 lock.lock()
 try {
 // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ
 return body()
 } finally {
 lock.unlock()
 }
 }

Slide 23

Slide 23 text

ߴ֊ؔ਺ // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ
 fun lock(lock: Lock, body: () -> T): T {
 lock.lock()
 try {
 // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ
 return body()
 } finally {
 lock.unlock()
 }
 }

Slide 24

Slide 24 text

ϥϜμࣜ

Slide 25

Slide 25 text

ϥϜμࣜ max(strings, { a, b -> a.length < b.length })
 max(strings) { a, b -> a.length < b.length }

Slide 26

Slide 26 text

֦ுؔ਺

Slide 27

Slide 27 text

֦ுؔ਺ "hoge".isEmpty()

Slide 28

Slide 28 text

֦ுؔ਺ "hoge".isEmpty() @kotlin.internal.InlineOnly
 public inline fun CharSequence.isEmpty(): Boolean = length == 0

Slide 29

Slide 29 text

֦ுؔ਺ "hoge".isEmpty() @kotlin.internal.InlineOnly
 public inline fun CharSequence.isEmpty(): Boolean = length == 0 Ϩγʔό

Slide 30

Slide 30 text

֦ுؔ਺ "hoge".isEmpty() @kotlin.internal.InlineOnly
 public inline fun CharSequence.isEmpty(): Boolean = length == 0 this.length: this ͸ϨγʔόΦϒδΣΫτʹରԠ

Slide 31

Slide 31 text

࿅श໰୊Ͱߟ͑ͯΈΔ

Slide 32

Slide 32 text

public String toJSON(Collection collection) {
 StringBuilder sb = new StringBuilder();
 sb.append("[");
 Iterator iterator = collection.iterator();
 while (iterator.hasNext()) {
 Integer element = iterator.next();
 sb.append(element);
 if (iterator.hasNext()) {
 sb.append(", ");
 }
 }
 sb.append("]");
 return sb.toString();
 }

Slide 33

Slide 33 text

౴͑

Slide 34

Slide 34 text

fun toJSON(collection: Collection): String {
 val sb = StringBuilder()
 sb.append("[")
 val iterator = collection.iterator()
 while (iterator.hasNext()) {
 val element = iterator.next()
 sb.append(element)
 if (iterator.hasNext()) {
 sb.append(", ")
 }
 }
 sb.append("]")
 return sb.toString()
 }

Slide 35

Slide 35 text

public String toJSON(Collection collection) {
 StringBuilder sb = new StringBuilder();
 sb.append("[");
 Iterator iterator = collection.iterator();
 while (iterator.hasNext()) {
 Integer element = iterator.next();
 sb.append(element);
 if (iterator.hasNext()) {
 sb.append(", ");
 }
 }
 sb.append("]");
 return sb.toString();
 }

Slide 36

Slide 36 text

είʔϓؔ਺

Slide 37

Slide 37 text

είʔϓؔ਺ୡ(ൈਮ) public inline fun T.run(block: T.() -> R): R = block() public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() public inline fun T.apply(block: T.() -> Unit): T { block(); return this } public inline fun T.also(block: (T) -> Unit): T { block(this); return this } public inline fun T.let(block: (T) -> R): R = block(this)

Slide 38

Slide 38 text

είʔϓؔ਺(run) public inline fun T.run(block: T.() -> R): R = block() val s = "hoge".run { toUpperCase() }
 println(s) //=> HOGE ೚ҙͷܕTΛϨγʔόͱ͠ɺ ೚ҙͷܕRΛฦؔ͢਺

Slide 39

Slide 39 text

είʔϓؔ਺(run) public inline fun T.run(block: T.() -> R): R = block() val s = "hoge".run { toUpperCase() }
 println(s) //=> HOGE ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ ʢলུ͍ͯ͠Δʣ

Slide 40

Slide 40 text

είʔϓؔ਺(with) public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() val s = with("hoge") { this.toUpperCase() }
 println(s) //=> HOGE

Slide 41

Slide 41 text

είʔϓؔ਺(with) public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() val s = with("hoge") { toUpperCase() }
 println(s) //=> HOGE ֦ுؔ਺Ͱ͸ͳ͍

Slide 42

Slide 42 text

είʔϓؔ਺(with) public inline fun with(receiver: T, block: T.() -> R): R = receiver.block() val s = with("hoge") { toUpperCase() }
 println(s) //=> HOGE ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ ʢলུ͍ͯ͠Δʣ

Slide 43

Slide 43 text

είʔϓؔ਺(apply) public inline fun T.apply(block: T.() -> Unit): T { block(); return this } val s = "hoge".apply { toUpperCase() }
 println(s) //=> hoge ໭Γ஋͸this ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎

Slide 44

Slide 44 text

είʔϓؔ਺(apply) public inline fun T.apply(block: T.() -> Unit): T { block(); return this } val s = "hoge".apply { toUpperCase() }
 println(s) //=> hoge toUpperCase()Λ͍ͯ͠Δ͕ɺ “hoge” ͕ฦ͞ΕΔͷͰ஫ҙ

Slide 45

Slide 45 text

είʔϓؔ਺(apply) view.layoutParams = view.layoutParams.apply { height = width / 2 }

Slide 46

Slide 46 text

είʔϓؔ਺(also) public inline fun T.also(block: (T) -> Unit): T { block(this); return this } val s = "hoge".also { it.toUpperCase() }
 println(s) //=> hoge ໭Γ஋͸this ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎

Slide 47

Slide 47 text

είʔϓؔ਺(also) public inline fun T.also(block: (T) -> Unit): T { block(this); return this } val s = "hoge".also { it.toUpperCase() }
 println(s) //=> hoge Ҿ਺ͱͯ͠T͕౉ͬͯ͘ΔͨΊɺ itͰΞΫηεՄೳ

Slide 48

Slide 48 text

είʔϓؔ਺(let) public inline fun T.let(block: (T) -> R): R = block(this) val upperCase: String? = foo?.let { it.toUpperCase() }

Slide 49

Slide 49 text

είʔϓؔ਺Λ༻͍ͯɺ ઌఔͷ໰୊Λ͞Βʹ؆ܿʹɻ

Slide 50

Slide 50 text

fun toJSON(collection: Collection): String {
 val sb = StringBuilder()
 sb.append("[")
 val iterator = collection.iterator()
 while (iterator.hasNext()) {
 val element = iterator.next()
 sb.append(element)
 if (iterator.hasNext()) {
 sb.append(", ")
 }
 }
 sb.append("]")
 return sb.toString()
 }

Slide 51

Slide 51 text

fun toJSON(collection: Collection): String =
 StringBuilder().run {
 append("[")
 collection.iterator().also {
 while (it.hasNext()) {
 append(it.next())
 if (it.hasNext()) {
 append(", ")
 }
 }
 }
 append("]")
 toString()
 }

Slide 52

Slide 52 text

͜Ε͔Βͷֶश

Slide 53

Slide 53 text

Next Step • Slack • kotlinlang: http://slack.kotlinlang.org/ • kotlinlang-jp: http://kotlinlang-jp.herokuapp.com/ • Try Kotlin: https://try.kotlinlang.org/ • σίϯύΠϧ • JS, Server: https://github.com/Kotlin/kotlin-fullstack-sample/ • ຊ • ॿ૸ಡຊʢແྉެ։ͷPDFʣ: https://goo.gl/5vUT7o • Kotlinʢ੺΂͜ຊʣ

Slide 54

Slide 54 text

͜Ε͔Βͷֶश • Slack • kotlinlang: http://slack.kotlinlang.org/ • kotlinlang-jp: http://kotlinlang-jp.herokuapp.com/ • Try Kotlin: https://try.kotlinlang.org/ • σίϯύΠϧ • JS, Server: https://github.com/Kotlin/kotlin-fullstack-sample/ • ຊ • ॿ૸ಡຊʢແྉެ։ͷPDFʣ: https://goo.gl/5vUT7o • Kotlinʢ੺΂͜ຊʣ