2017_11_14_Kotlin_Conf_Report_Kotlin_Types

Daec7e5cd5fae384eda88037d937343b?s=47 AAkira
November 14, 2017

 2017_11_14_Kotlin_Conf_Report_Kotlin_Types

CA.kt #4 Kotlin Conf Report
Kotlin Types
This presentation based on Kotlin Exposed Type by Svetlana Isakova in Kotlin Conf 2017.

https://cyberagent.connpass.com/event/70423/

Daec7e5cd5fae384eda88037d937343b?s=128

AAkira

November 14, 2017
Tweet

Transcript

  1. Kotlin Conf報告会 @ CA.kt #4 AAkira - Kotlin Types -

  2. @_a_akira AAkira CyberAgent, Inc. Akira Aratani private lateinit var aakira

    : User data class User(val name: String, val twitterId: String, val githubId: String, val company: String) print("Name : ${aakira.name}”) println("Github Id : ${aakira.githubId}") print("Twitter Id : ${aakira.twitterId}") println("Company : ${aakira.company}") $ whois
  3. About • 生放送配信プラットフォーム ≠ AbemaTV ≒ • M11の頃からFull Kotlinで開発 • サーバサイドも1年前からKotlinを使用

  4. 私とKotlin M1 2012-04-12 M11 2015-03-19 M14 2015-10-01 1.0-beta4 2015-12-22 M13

    2015-09-16 1.0 2016-02-16 1.0-RC 2016-02-04 2016-01-21 Release 2015-04 開発開始 kotlin FRESH 1.1 2017-03-01 2017-05-15 Renewal
  5. None
  6. None
  7. Svetlana Isakova @JetBrains Kotlin in Action の著者の1人

  8. None
  9. Agenda • Basic types • Nullable types • Collection types

  10. Agenda • Basic types • Nullable types • Collection types

    • Kotlin/Java types • Unit, Nothing, void • Collections →
  11. Kotlin types Java types

  12. Kotlin, Java Types fun foo(): Int = 1 fun bar():

    Int? = 1
  13. Kotlin, Java Types

  14. Kotlin, Java Types public static final int foo() { return

    1; } @Nullable public static final Integer bar() { return Integer.valueOf(1); }
  15. Kotlin, Java Types public static final int foo() { return

    1; } @Nullable public static final Integer bar() { return Integer.valueOf(1); }
  16. Kotlin, Java Types Int → int Int? → java.lang.Integer

  17. Kotlin, Java Types Double → int Double? → java.lang.Integer Boolean

    → boolean Boolean? → java.lang.Boolean
  18. Kotlin, Java Types List<Int> → List<Integer> kotlin.String → java.lang.String Array<Int>

    → Integer[]
  19. Kotlin, Java Types List<Int> → List<Integer> kotlin.String → java.lang.String Array<Int>

    → Integer[]
  20. Kotlin, Java Types "one.two".replaceAll(".", "*")

  21. Kotlin, Java Types "one.two".replaceAll(".", "*") ********

  22. Kotlin, Java Types "one.two".replaceAll(".", "*") "one.two".replace(".", "*") ********

  23. Kotlin, Java Types "one.two".replaceAll(".", "*") "one.two".replace(".", "*") ******** one*two

  24. Kotlin, Java Types "one.two".replace(".".toRegex(), "*") "one.two".replaceAll(".", "*") "one.two".replace(".", "*") ********

    one*two
  25. Unit, Nothing, void

  26. Unit, Nothing, void void Unit Nothing

  27. Unit, Nothing, void void Unit Nothing ≠ ,

  28. Unit, Nothing, void void Unit

  29. Unit, Nothing, void Unit The type with only one value:

    the Unit object. This type corresponds to the `void` type in Java.
  30. Unit, Nothing, void void Unit ⇨ 意味のない値を返す =

  31. Unit, Nothing, void Nothing has no instances. You can use

    Nothing to represent "a value that never exists”: for example, if a function has the return type of Nothing,
 it means that it never returns (always throws an exception). Nothing
  32. Unit, Nothing, void Nothing ⇨ 存在しないを表す, 値は返さない

  33. Unit, Nothing, void val foo = if (bar) { 123

    } else { fail() } fun fail() { throw IllegalStateException() }
  34. Unit, Nothing, void val foo = if (bar) { 123

    } else { fail() } fun fail(): Unit { throw IllegalStateException() }
  35. Unit, Nothing, void val foo = if (bar) { 123

    } else { fail() } fun fail(): Unit { throw IllegalStateException() }
  36. Unit, Nothing, void val foo = if (bar) { 123

    } else { fail() } fun fail(): Unit { throw IllegalStateException() } Int Unit
  37. Unit, Nothing, void Int Unit Any

  38. Unit, Nothing, void val foo: Any = if (bar) {

    123 } else { fail() } fun fail(): Unit { throw IllegalStateException() }
  39. Kotlin, Java Types Any → java.lang.Object

  40. Unit, Nothing, void val foo: Any = if (bar) {

    123 } else { fail() } fun fail(): Unit { throw IllegalStateException() } ⇨ fooはInt型にしたい
  41. Unit, Nothing, void val foo = if (bar) { 123

    } else { fail() } fun fail(): Nothing { throw IllegalStateException() }
  42. Unit, Nothing, void val foo = if (bar) { 123

    } else { fail() } fun fail(): Nothing { throw IllegalStateException() } Int Nothing
  43. Unit, Nothing, void Int Unit Any Nothing

  44. Unit, Nothing, void val foo: Int = if (bar) {

    123 } else { fail() } fun fail(): Nothing { throw IllegalStateException() }
  45. Unit, Nothing, void val foo: Int = if (bar) {

    123 } else { fail() } fun fail(): Nothing { throw IllegalStateException() }
  46. Collections

  47. Collections kotlin.MutableList kotlin.List

  48. Collections val mutableList = mutableListOf(1, 2, 3) val list: List<Int>

    = mutableList println(list) // [1, 2, 3]
  49. Collections val mutableList = mutableListOf(1, 2, 3) val list: List<Int>

    = mutableList println(list) // [1, 2, 3] mutableList.add(4) println(list)
  50. Collections val mutableList = mutableListOf(1, 2, 3) val list: List<Int>

    = mutableList println(list) // [1, 2, 3] mutableList.add(4) println(list) // [1, 2, 3, 4]
  51. Collections ⇨ Read-only ≠ immutable mutableList.add(4) println(list) // [1, 2,

    3, 4]
  52. Collections fun foo(): List<Int> = listOf(1) fun bar(): MutableList<Int> =

    mutableListOf(1) ⇨ Decompile
  53. Collections @NotNull public static final List foo() { return CollectionsKt.listOf(Integer.valueOf(1));

    } @NotNull public static final List bar() { return CollectionsKt.mutableListOf(
 new Integer[]{Integer.valueOf(1)}); }
  54. Collections @NotNull public static final List foo() { return CollectionsKt.listOf(Integer.valueOf(1));

    } @NotNull public static final List bar() { return CollectionsKt.mutableListOf(
 new Integer[]{Integer.valueOf(1)}); }
  55. Collections kotlin.MutableList kotlin.List java.util.ArrayList

  56. Summary

  57. Summary • Decompile to Java code • Nothing ≠ Unit,

    void • Read-only ≠ immutable
  58. Have a nice Kotlin!