Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin入門

 Kotlin入門

スコープ関数の理解をゴールとしたKotlinの入門資料

magiepooh

June 14, 2017
Tweet

More Decks by magiepooh

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

  4. View Slide

  5. What’s Kotlin?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. ม਺

    View Slide

  12. val num : Int = 1

    var num2 : Int = 2


    num = 3 // error

    num2 = 3
    ม਺ͷએݴ

    View Slide

  13. val num = 1

    var num2 = 2


    num = 3 // error

    num2 = 3
    ม਺ͷએݴ

    View Slide

  14. Null҆શ
    var a: String = "abc"

    a = null // => ίϯύΠϧΤϥʔ


    var b: String? = "abc"

    b = null // => OK

    View Slide

  15. 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

    View Slide

  16. Null҆શʢΤϧϏεԋࢉࢠʣ
    var b: String? = "abc"

    val l : Int = if(b != null)
    b.length else -1
    val l : Int = b?.length ?: -1

    View Slide

  17. ؔ਺

    View Slide

  18. ؔ਺ͷجຊ
    fun add(x: Int, y: Int): Int {

    return x + y

    }

    View Slide

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

    View Slide

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

    View Slide

  21. ߴ֊ؔ਺

    View Slide

  22. ߴ֊ؔ਺
    // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ

    fun lock(lock: Lock, body: () -> T): T {

    lock.lock()

    try {

    // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ

    return body()

    } finally {

    lock.unlock()

    }

    }

    View Slide

  23. ߴ֊ؔ਺
    // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ

    fun lock(lock: Lock, body: () -> T): T {

    lock.lock()

    try {

    // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ

    return body()

    } finally {

    lock.unlock()

    }

    }

    View Slide

  24. ϥϜμࣜ

    View Slide

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

    max(strings) { a, b -> a.length < b.length }

    View Slide

  26. ֦ுؔ਺

    View Slide

  27. ֦ுؔ਺
    "hoge".isEmpty()

    View Slide

  28. ֦ுؔ਺
    "hoge".isEmpty()
    @kotlin.internal.InlineOnly

    public inline fun CharSequence.isEmpty():
    Boolean = length == 0

    View Slide

  29. ֦ுؔ਺
    "hoge".isEmpty()
    @kotlin.internal.InlineOnly

    public inline fun CharSequence.isEmpty():
    Boolean = length == 0
    Ϩγʔό

    View Slide

  30. ֦ுؔ਺
    "hoge".isEmpty()
    @kotlin.internal.InlineOnly

    public inline fun CharSequence.isEmpty():
    Boolean = length == 0
    this.length:
    this ͸ϨγʔόΦϒδΣΫτʹରԠ

    View Slide

  31. ࿅श໰୊Ͱߟ͑ͯΈΔ

    View Slide

  32. 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();

    }

    View Slide

  33. ౴͑

    View Slide

  34. 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()

    }

    View Slide

  35. 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();

    }

    View Slide

  36. είʔϓؔ਺

    View Slide

  37. είʔϓؔ਺ୡ(ൈਮ)
    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)

    View Slide

  38. είʔϓؔ਺(run)
    public inline fun T.run(block: T.() -> R): R = block()
    val s = "hoge".run { toUpperCase() }

    println(s) //=> HOGE
    ೚ҙͷܕTΛϨγʔόͱ͠ɺ
    ೚ҙͷܕRΛฦؔ͢਺

    View Slide

  39. είʔϓؔ਺(run)
    public inline fun T.run(block: T.() -> R): R = block()
    val s = "hoge".run { toUpperCase() }

    println(s) //=> HOGE
    ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ
    ʢলུ͍ͯ͠Δʣ

    View Slide

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

    println(s) //=> HOGE

    View Slide

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

    println(s) //=> HOGE
    ֦ுؔ਺Ͱ͸ͳ͍

    View Slide

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

    println(s) //=> HOGE
    ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ
    ʢলུ͍ͯ͠Δʣ

    View Slide

  43. είʔϓؔ਺(apply)
    public inline fun T.apply(block: T.() -> Unit): T { block();
    return this }
    val s = "hoge".apply { toUpperCase() }

    println(s) //=> hoge
    ໭Γ஋͸this
    ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎

    View Slide

  44. είʔϓؔ਺(apply)
    public inline fun T.apply(block: T.() -> Unit): T { block();
    return this }
    val s = "hoge".apply { toUpperCase() }

    println(s) //=> hoge
    toUpperCase()Λ͍ͯ͠Δ͕ɺ
    “hoge” ͕ฦ͞ΕΔͷͰ஫ҙ

    View Slide

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

    View Slide

  46. είʔϓؔ਺(also)
    public inline fun T.also(block: (T) -> Unit): T { block(this);
    return this }
    val s = "hoge".also { it.toUpperCase() }

    println(s) //=> hoge
    ໭Γ஋͸this
    ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎

    View Slide

  47. είʔϓؔ਺(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ͰΞΫηεՄೳ

    View Slide

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

    View Slide

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

    View Slide

  50. 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()

    }

    View Slide

  51. fun toJSON(collection: Collection):
    String =

    StringBuilder().run {

    append("[")

    collection.iterator().also {

    while (it.hasNext()) {

    append(it.next())

    if (it.hasNext()) {

    append(", ")

    }

    }

    }

    append("]")

    toString()

    }

    View Slide

  52. ͜Ε͔Βͷֶश

    View Slide

  53. 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ʢ੺΂͜ຊʣ

    View Slide

  54. ͜Ε͔Βͷֶश
    • 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ʢ੺΂͜ຊʣ

    View Slide