Kotlin入門

 Kotlin入門

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

F958fbf402a67a1742335a4894e041e5?s=128

magiepooh

June 14, 2017
Tweet

Transcript

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

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

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

  4. None
  5. What’s Kotlin?

  6. Kotlin • Jet Brains ͕࡞ͬͨJVMݴޠ • ੩తܕ෇͚ͷΦϒδΣΫτࢦ޲ݴޠ • GoogleIOͰAndroidͷ։ൃݴޠͱͯ͠ެࣜαϙʔτ͕ൃද •

    2012೥ࠒ M1ϦϦʔε • 2016೥ 1.0 ϦϦʔε • Support JS(Kotlin/JS) • LLVM(Kotlin/Native)
  7. Kotlin • Lambdas • Nullable(NonNull) • High-Order Functions(Stream) • Extension

    Functions
  8. Kotlin • Lambdas -> Retrolambda • Nullable(NonNull) -> Optional •

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

    High-Order Functions(Stream) -> StremeAPI, RxJava • Extension Functions
  10. ࠓ೔ͷΰʔϧ • είʔϓؔ਺ͷཧղ • apply • let • run •

    with
  11. ม਺

  12. val num : Int = 1
 var num2 : Int

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

    = 3 // error
 num2 = 3 ม਺ͷએݴ
  14. Null҆શ var a: String = "abc"
 a = null //

    => ίϯύΠϧΤϥʔ
 
 var b: String? = "abc"
 b = null // => OK
  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
  16. Null҆શʢΤϧϏεԋࢉࢠʣ var b: String? = "abc"
 val l : Int

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

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

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

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

  21. ߴ֊ؔ਺

  22. ߴ֊ؔ਺ // body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ
 fun <T> lock(lock: Lock, body: ()

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

    -> T): T {
 lock.lock()
 try {
 // Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ
 return body()
 } finally {
 lock.unlock()
 }
 }
  24. ϥϜμࣜ

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


    max(strings) { a, b -> a.length < b.length }
  26. ֦ுؔ਺

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

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

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

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

    == 0 this.length: this ͸ϨγʔόΦϒδΣΫτʹରԠ
  31. ࿅श໰୊Ͱߟ͑ͯΈΔ

  32. public String toJSON(Collection<Integer> collection) {
 StringBuilder sb = new StringBuilder();


    sb.append("[");
 Iterator<Integer> iterator = collection.iterator();
 while (iterator.hasNext()) {
 Integer element = iterator.next();
 sb.append(element);
 if (iterator.hasNext()) {
 sb.append(", ");
 }
 }
 sb.append("]");
 return sb.toString();
 }
  33. ౴͑

  34. fun toJSON(collection: Collection<Int>): 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()
 }
  35. public String toJSON(Collection<Integer> collection) {
 StringBuilder sb = new StringBuilder();


    sb.append("[");
 Iterator<Integer> iterator = collection.iterator();
 while (iterator.hasNext()) {
 Integer element = iterator.next();
 sb.append(element);
 if (iterator.hasNext()) {
 sb.append(", ");
 }
 }
 sb.append("]");
 return sb.toString();
 }
  36. είʔϓؔ਺

  37. είʔϓؔ਺ୡ(ൈਮ) public inline fun <T, R> T.run(block: T.() -> R):

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

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

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

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

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

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

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

    { block(); return this } val s = "hoge".apply { toUpperCase() }
 println(s) //=> hoge toUpperCase()Λ͍ͯ͠Δ͕ɺ “hoge” ͕ฦ͞ΕΔͷͰ஫ҙ
  45. είʔϓؔ਺(apply) view.layoutParams = view.layoutParams.apply { height = width / 2

    }
  46. είʔϓؔ਺(also) public inline fun <T> T.also(block: (T) -> Unit): T

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

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

    R = block(this) val upperCase: String? = foo?.let { it.toUpperCase() }
  49. είʔϓؔ਺Λ༻͍ͯɺ ઌఔͷ໰୊Λ͞Βʹ؆ܿʹɻ

  50. fun toJSON(collection: Collection<Int>): 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()
 }
  51. fun toJSON(collection: Collection<Int>): String =
 StringBuilder().run {
 append("[")
 collection.iterator().also {


    while (it.hasNext()) {
 append(it.next())
 if (it.hasNext()) {
 append(", ")
 }
 }
 }
 append("]")
 toString()
 }
  52. ͜Ε͔Βͷֶश

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