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

2017_12_18-19_Kotlin_Conf_Report_Kotlin_Types_Updated

AAkira
December 18, 2017

 2017_12_18-19_Kotlin_Conf_Report_Kotlin_Types_Updated

CA.kt #5, 6 Kotlin Conf Report in Osaka, Fukuoka
Kotlin Types
This presentation based on Kotlin Exposed Type by Svetlana Isakova in Kotlin Conf 2017.

https://cyberagent.connpass.com/event/73540/
https://cyberagent.connpass.com/event/73541/

AAkira

December 18, 2017
Tweet

More Decks by AAkira

Other Decks in Technology

Transcript

  1. Kotlin Conf報告会
    @ CA.kt #5, 6
    AAkira
    - Kotlin Types -

    View Slide

  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

    View Slide

  3. About
    • 生放送配信プラットフォーム ≠ AbemaTV ≒
    • M11の頃からFull Kotlinで開発
    • サーバサイドも1年前からKotlinを使用

    View Slide

  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

    View Slide

  5. View Slide

  6. View Slide

  7. Svetlana Isakova
    @JetBrains
    Kotlin in Action の著者の1人

    View Slide

  8. View Slide

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

    View Slide

  10. Agenda
    • Basic types
    • Nullable types
    • Collection types
    • Kotlin/Java types
    • Any, Unit, Nothing, void
    • Collections

    View Slide

  11. Kotlin types
    Java types

    View Slide

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

    View Slide

  13. Kotlin, Java Types

    View Slide

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

    View Slide

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

    View Slide

  16. Kotlin, Java Types
    Int → int
    Int? → java.lang.Integer

    View Slide

  17. Kotlin, Java Types
    Double → double
    Double? → java.lang.Double
    Boolean → boolean
    Boolean? → java.lang.Boolean

    View Slide

  18. Kotlin, Java Types
    List → java.util.List
    kotlin.String → java.lang.String
    Array → Integer[]

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  25. Any, Unit, Nothing, void

    View Slide

  26. Any java.lang.Object
    Any, Unit, Nothing, void

    View Slide

  27. Any, Unit, Nothing, void
    The root of the Kotlin class hierarchy.
    Every Kotlin class has [Any] as a superclass.
    Any

    View Slide

  28. Any, Unit, Nothing, void
    log(2017)
    fun log(any: Any) {
    println("Value: $any")
    }
    fun log(i: Int) {
    println("Value: $i”)
    }

    View Slide

  29. Any, Unit, Nothing, void
    log(2017)
    fun log(any: Any) {
    println("Value: $any")
    }
    fun log(i: Int) {
    println("Value: $i”)
    }
    Auto boxing

    View Slide

  30. Any, Unit, Nothing, void
    fun log(i: Int) {
    println("Value: $i”)
    }

    View Slide

  31. Any, Unit, Nothing, void
    fun log(any: Any) {
    println("Value: $any")
    }

    View Slide

  32. Any, Unit, Nothing, void
    Int String
    Any

    View Slide

  33. val hoge: Int? = 2017
    log(hoge)
    fun log(any: Any) {
    println("Value: $any")
    }
    Any, Unit, Nothing, void

    View Slide

  34. val hoge: Int? = 2017
    log(hoge)
    fun log(any: Any) {
    println("Value: $any")
    }
    Any, Unit, Nothing, void
    ⇨ Compile Error

    View Slide

  35. Any, Unit, Nothing, void
    Any Nullable Type
    Any?
    Non-null Type

    View Slide

  36. Any, Unit, Nothing, void
    void
    Unit
    Nothing

    View Slide

  37. Any, Unit, Nothing, void
    void
    Unit
    Nothing ≠ ,

    View Slide

  38. Any, Unit, Nothing, void
    void
    Unit

    View Slide

  39. Any, Unit, Nothing, void
    Unit
    The type with only one value: the Unit object.
    This type corresponds to the `void` type in Java.

    View Slide

  40. Any, Unit, Nothing, void
    void
    Unit
    ⇨ 意味のない値を返す
    =

    View Slide

  41. Any, 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

    View Slide

  42. Any, Unit, Nothing, void
    Nothing
    ⇨ 存在しないを表す, 値は返さない

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  47. Any, Unit, Nothing, void
    Int Unit
    Any

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  52. Any, Unit, Nothing, void
    Int Unit
    Any
    Nothing

    View Slide

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

    View Slide

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

    View Slide

  55. Nothing?
    Any, Unit, Nothing, void

    View Slide

  56. Any, Unit, Nothing, void
    Int Unit
    Any
    Nothing

    View Slide

  57. Any, Unit, Nothing, void
    Non-null Type
    Any
    Nothing
    Non-null Type

    View Slide

  58. Any, Unit, Nothing, void
    Any
    Any?
    Non-null Type
    Nothing

    View Slide

  59. Any, Unit, Nothing, void
    Any Nullable Type
    Any?
    Non-null Type
    Nothing

    View Slide

  60. Any, Unit, Nothing, void
    Any Nullable Type
    Any?
    Non-null Type Nothing?
    Nothing

    View Slide

  61. Collections

    View Slide

  62. Collections
    kotlin.MutableList
    kotlin.List

    View Slide

  63. Collections
    val mutableList = mutableListOf(1, 2, 3)
    val list: List = mutableList
    println(list) // [1, 2, 3]

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  67. Collections
    fun foo(): List = listOf(1)
    fun bar(): MutableList = mutableListOf(1)
    ⇨ Decompile

    View Slide

  68. 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)});
    }

    View Slide

  69. 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)});
    }

    View Slide

  70. Collections
    kotlin.MutableList
    kotlin.List
    java.util.ArrayList

    View Slide

  71. Summary

    View Slide

  72. Summary
    • Decompile to Java code
    • Nothing ≠ Unit, void
    • Read-only ≠ immutable

    View Slide

  73. Have a nice Kotlin!

    View Slide