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

Top 10 Kotlin Stack Overflow Questions (Google I/O Extended 2018 Budapest)

Top 10 Kotlin Stack Overflow Questions (Google I/O Extended 2018 Budapest)

A selection of some of the most frequent questions about Kotlin on Stack Overflow, asked, answered and explained. Something in there for everyone, from beginners to experts.

Talk recording (Hungarian): https://youtu.be/-UMqYBoUlJc?t=4h7m38s

Marton Braun

May 08, 2018
Tweet

More Decks by Marton Braun

Other Decks in Programming

Transcript

  1. TOP 10 KOTLIN
    STACK OVERFLOW
    QUESTIONS
    Márton Szabolcs Braun
    zsmb.co zsmb13
    [email protected]

    View full-size slide

  2. Why do this talk?

    View full-size slide

  3. Why do this talk?

    View full-size slide

  4. Array vs
    IntArray
    What’s the difference? When do I use which one?

    View full-size slide

  5. Array vs IntArray

    View full-size slide

  6. Array vs IntArray
    Array

    View full-size slide

  7. Array vs IntArray
    Array Integer[]

    View full-size slide

  8. Array vs IntArray
    Array Integer[]
    val arrayOfInts: Array = arrayOf(1, 2, 3, 4, 5)

    View full-size slide

  9. Array vs IntArray
    Array Integer[]
    IntArray
    val arrayOfInts: Array = arrayOf(1, 2, 3, 4, 5)

    View full-size slide

  10. Array vs IntArray
    Array Integer[]
    IntArray int[]
    val arrayOfInts: Array = arrayOf(1, 2, 3, 4, 5)

    View full-size slide

  11. Array vs IntArray
    Array Integer[]
    IntArray int[]
    val intArray: IntArray = intArrayOf(1, 2, 3, 4, 5)
    val arrayOfInts: Array = arrayOf(1, 2, 3, 4, 5)

    View full-size slide

  12. Array vs IntArray

    View full-size slide

  13. Array vs IntArray
    • IntArray

    View full-size slide

  14. Array vs IntArray
    • IntArray
     Avoids boxing

    View full-size slide

  15. Array vs IntArray
    • IntArray
     Avoids boxing
     Easier to initialize
    val intArray = IntArray(10)
    val arrayOfInts = Array(5) { i -> i * 2 }

    View full-size slide

  16. Array vs IntArray
    • IntArray
     Avoids boxing
     Easier to initialize
    • Array
    val intArray = IntArray(10)
    val arrayOfInts = Array(5) { i -> i * 2 }

    View full-size slide

  17. Array vs IntArray
    • IntArray
     Avoids boxing
     Easier to initialize
    • Array
     Might be required by an API
    val intArray = IntArray(10)
    val arrayOfInts = Array(5) { i -> i * 2 }

    View full-size slide

  18. Array vs IntArray
    • IntArray
     Avoids boxing
     Easier to initialize
    • Array
     Might be required by an API
     Can store nullable values
    val intArray = IntArray(10)
    val arrayOfInts = Array(5) { i -> i * 2 }
    val notPeople: Array = arrayOfNulls(13)

    View full-size slide

  19. Iterable vs
    Sequence
    What’s the difference? When do I use which one?

    View full-size slide

  20. Iterable vs Sequence

    View full-size slide

  21. Iterable vs Sequence
    Iterable

    View full-size slide

  22. Iterable vs Sequence
    Iterable java.lang.Iterable

    View full-size slide

  23. Iterable vs Sequence
     Eager evaluation
    Iterable java.lang.Iterable

    View full-size slide

  24. Iterable vs Sequence
     Eager evaluation
    Iterable java.lang.Iterable
    val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)

    View full-size slide

  25. Iterable vs Sequence

    View full-size slide

  26. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  27. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  28. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  29. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  30. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  31. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  32. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  33. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  34. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  35. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  36. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  37. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  38. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  39. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  40. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  41. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  42. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  43. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  44. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  45. val people: List = getPeople()
    val allowedEntrance = people
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    Iterable vs Sequence

    View full-size slide

  46. Iterable vs Sequence

    View full-size slide

  47. Iterable vs Sequence
    Sequence

    View full-size slide

  48. Iterable vs Sequence
    Sequence
     Lazy evaluation

    View full-size slide

  49. Iterable vs Sequence
    Sequence
     Lazy evaluation
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  50. Iterable vs Sequence
    Sequence
     Lazy evaluation
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  51. Iterable vs Sequence

    View full-size slide

  52. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  53. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  54. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  55. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  56. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  57. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  58. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  59. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  60. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  61. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  62. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  63. Iterable vs Sequence
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  64. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  65. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  66. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  67. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  68. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  69. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  70. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  71. Iterable vs Sequence
    val people: List = getPeople()
    val allowedEntrance = people.asSequence()
    .filter { it.age >= 21 }
    .map { it.name }
    .take(5)
    .toList()

    View full-size slide

  72. Iterable vs Sequence

    View full-size slide

  73. Iterable vs Sequence
    • Iterable

    View full-size slide

  74. Iterable vs Sequence
    • Iterable
     Use by default

    View full-size slide

  75. Iterable vs Sequence
    • Iterable
     Use by default
     Usually the best choice

    View full-size slide

  76. Iterable vs Sequence
    • Iterable
     Use by default
     Usually the best choice
    • Sequence

    View full-size slide

  77. Iterable vs Sequence
    • Iterable
     Use by default
     Usually the best choice
    • Sequence
     To handle an infinite number of elements

    View full-size slide

  78. Iterable vs Sequence
    • Iterable
     Use by default
     Usually the best choice
    • Sequence
     To handle an infinite number of elements
     For huge collections, judiciously

    View full-size slide

  79. Iterable vs Sequence
    • Iterable
     Use by default
     Usually the best choice
    • Sequence
     To handle an infinite number of elements
     For huge collections, judiciously
    • Stream

    View full-size slide

  80. Iterable vs Sequence
    • Iterable
     Use by default
     Usually the best choice
    • Sequence
     To handle an infinite number of elements
     For huge collections, judiciously
    • Stream
     When interoperating with Java

    View full-size slide

  81. Iteration
    How can / should I iterate over collections?

    View full-size slide

  82. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }

    View full-size slide

  83. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }
    for (i in 0..args.lastIndex) {
    println(args[i])
    }

    View full-size slide

  84. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }
    for (i in 0..args.lastIndex) {
    println(args[i])
    }
    for (i in 0 until args.size) {
    println(args[i])
    }

    View full-size slide

  85. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }
    for (i in 0..args.lastIndex) {
    println(args[i])
    }
    for (i in 0 until args.size) {
    println(args[i])
    }
    for (i in args.indices) {
    println(args[i])
    }

    View full-size slide

  86. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }
    for (i in 0..args.lastIndex) {
    println(args[i])
    }
    for (i in 0 until args.size) {
    println(args[i])
    }
    for (i in args.indices) {
    println(args[i])
    }
    for (arg in args) {
    println(arg)
    }

    View full-size slide

  87. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }
    for (i in 0..args.lastIndex) {
    println(args[i])
    }
    for (i in 0 until args.size) {
    println(args[i])
    }
    for (i in args.indices) {
    println(args[i])
    }
    for (arg in args) {
    println(arg)
    }
    args.forEach { arg ->
    println(arg)
    }

    View full-size slide

  88. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }
    for (i in 0..args.lastIndex) {
    println(args[i])
    }
    for (i in 0 until args.size) {
    println(args[i])
    }
    for (i in args.indices) {
    println(args[i])
    }
    for (arg in args) {
    println(arg)
    }
    args.forEach { arg ->
    println(arg)
    }

    View full-size slide

  89. Iteration
    for (i in 0..args.size - 1) {
    println(args[i])
    }
    for (i in 0..args.lastIndex) {
    println(args[i])
    }
    for (i in 0 until args.size) {
    println(args[i])
    }
    for (i in args.indices) {
    println(args[i])
    }
    for (arg in args) {
    println(arg)
    }
    args.forEach { arg ->
    println(arg)
    }

    View full-size slide

  90. SAM conversions
    What are SAM conversions?

    View full-size slide

  91. SAM conversions

    View full-size slide

  92. SAM conversions
    public interface OnClickListener {
    void onClick(Button button);
    }

    View full-size slide

  93. SAM conversions
    public interface OnClickListener {
    void onClick(Button button);
    }
    public class Button {
    public void setListener(OnClickListener listener) {
    ...
    }
    }

    View full-size slide

  94. SAM conversions

    View full-size slide

  95. SAM conversions
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    println("Clicked!")
    }
    })

    View full-size slide

  96. SAM conversions
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    println("Clicked!")
    }
    })
    button.setListener {
    println("Clicked!")
    }

    View full-size slide

  97. SAM conversions
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    println("Clicked!")
    }
    })
    button.setListener {
    println("Clicked!")
    }

    View full-size slide

  98. SAM conversions
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    println("Clicked!")
    }
    })
    button.setListener { button: Button? ->
    println("Clicked!")
    }

    View full-size slide

  99. SAM conversions #1
    Why are my listeners not being called?

    View full-size slide

  100. SAM conversions #1
    button.setListener {
    object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    }

    View full-size slide

  101. SAM conversions #1
    button.setListener {
    object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    }

    View full-size slide

  102. SAM conversions #1
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    }
    })

    View full-size slide

  103. SAM conversions #1
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    val listener = object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    }
    })

    View full-size slide

  104. SAM conversions #1
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    val listener = object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    listener.onClick(...)
    }
    })

    View full-size slide

  105. SAM conversions #1
    button.setListener(object : OnClickListener {
    override fun onClick(button: Button?) {
    object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    }
    })

    View full-size slide

  106. SAM conversions #1
    button.setListener {
    object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    }

    View full-size slide

  107. SAM conversions #1
    button.setListener {
    object : OnClickListener {
    override fun onClick(button: Button) {
    println("Clicked!")
    }
    })
    }
    button.setListener {
    fun onClick(button: Button) {
    println("Clicked!")
    }
    }

    View full-size slide

  108. SAM conversions #2
    Why am I getting a return type error?

    View full-size slide

  109. SAM conversions #2
    public interface OnClickListener {
    void onClick(Button button);
    }

    View full-size slide

  110. SAM conversions #2
    public interface OnClickListener {
    boolean onClick(Button button);
    }

    View full-size slide

  111. SAM conversions #2
    public interface OnClickListener {
    boolean onClick(Button button);
    }
    button.setListener {
    println("Clicked!")
    }

    View full-size slide

  112. SAM conversions #2
    public interface OnClickListener {
    boolean onClick(Button button);
    }
    button.setListener {
    println("Clicked!")
    }
    Kotlin: Type mismatch: inferred type is Unit but
    Boolean was expected

    View full-size slide

  113. SAM conversions #2
    public interface OnClickListener {
    boolean onClick(Button button);
    }
    button.setListener {
    println("Clicked!")
    true
    }

    View full-size slide

  114. SAM conversions #2
    public interface OnClickListener {
    boolean onClick(Button button);
    }
    button.setListener {
    println("Clicked!")
    true
    }

    View full-size slide

  115. Replacing statics
    How do I create static functions and variables in Kotlin?

    View full-size slide

  116. Replacing statics
    class Foo {
    companion object {
    fun x() { ... }
    }
    fun y() { ... }
    }

    View full-size slide

  117. Replacing statics
    class Foo {
    companion object {
    fun x() { ... }
    }
    fun y() { ... }
    }
    Foo.x()

    View full-size slide

  118. Replacing statics
    class Foo {
    companion object {
    fun x() { ... }
    }
    fun y() { ... }
    }
    object Foo {
    fun x() { ... }
    }
    Foo.x()
    Foo.x()

    View full-size slide

  119. Replacing statics
    class Foo {
    companion object {
    fun x() { ... }
    }
    fun y() { ... }
    }
    object Foo {
    fun x() { ... }
    }
    fun x() { ... }
    Foo.x()
    Foo.x()
    x()

    View full-size slide

  120. Replacing statics
    class Foo {
    companion object {
    fun x() { ... }
    }
    fun y() { ... }
    }
    object Foo {
    fun x() { ... }
    }
    fun x() { ... }
    Foo.x()
    Foo.x()
    x()

    View full-size slide

  121. Function declaration reference
    Declaration Kotlin usage Java usage
    Companion object Foo.f() Foo.Companion.f();
    Companion object with @JvmStatic Foo.f() Foo.f();
    Object Foo.f() Foo.INSTANCE.f();
    Object with @JvmStatic Foo.f() Foo.f();
    Top level function f() UtilKt.f();
    Top level function with @JvmName* f() Util.f();
    * With the @JvmName annotation on the file use-site target

    View full-size slide

  122. Variable declaration reference
    Declaration Kotlin usage Java usage
    Companion object X.x X.Companion.getX();
    Companion object with @JvmStatic X.x X.getX();
    Companion object with @JvmField X.x X.x;
    Companion object with const X.x X.x;
    Object X.x X.INSTANCE.getX();
    Object with @JvmStatic X.x X.getX();
    Object with @JvmField X.x X.x;
    Object with const X.x X.x;
    Top level variable x ConstKt.getX();
    Top level variable with @JvmField x ConstKt.x;
    Top level variable with const x ConstKt.x;
    Top level variable with @JvmName* x Const.getX();
    Top level variable with @JvmName* and @JvmField x Const.x;
    Top level variable with @JvmName* and const x Const.x;
    * With the @JvmName annotation on the file use-site target

    View full-size slide

  123. Smart casts on
    mutable properties
    Why are smart casts not working?

    View full-size slide

  124. Smart casts on mutable properties
    class Dog(var toy: Toy? = null) {
    fun play() {
    if (toy != null) {
    toy.chew()
    }
    }
    }

    View full-size slide

  125. Smart casts on mutable properties
    class Dog(var toy: Toy? = null) {
    fun play() {
    if (toy != null) {
    toy.chew()
    }
    }
    }
    Kotlin: Smart cast to 'Toy' is impossible, because 'toy' is
    a mutable property that could have been changed by this time

    View full-size slide

  126. Smart casts on mutable properties
    class Dog(val toy: Toy? = null) {
    fun play() {
    if (toy != null) {
    toy.chew()
    }
    }
    }

    View full-size slide

  127. Smart casts on mutable properties
    class Dog(var toy: Toy? = null) {
    fun play() {
    if (toy != null) {
    toy.chew()
    }
    }
    }

    View full-size slide

  128. Smart casts on mutable properties
    class Dog(var toy: Toy? = null) {
    fun play() {
    val _toy = toy
    if (_toy != null) {
    _toy.chew()
    }
    }
    }

    View full-size slide

  129. Smart casts on mutable properties
    class Dog(var toy: Toy? = null) {
    fun play() {
    toy?.let {
    it.chew()
    }
    }
    }

    View full-size slide

  130. Smart casts on mutable properties
    class Dog(var toy: Toy? = null) {
    fun play() {
    toy?.chew()
    }
    }

    View full-size slide

  131. Smart casts on mutable properties
    class Dog(var toy: Toy? = null) {
    fun play() {
    toy?.let {
    it.chew()
    it.chew()
    it.chew()
    }
    }
    }

    View full-size slide

  132. null!!
    Why am I getting NullPointerExceptions?

    View full-size slide

  133. null!!
    class Episode {
    var airdate: Date = null!!
    }

    View full-size slide

  134. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }

    View full-size slide

  135. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }
    Date?

    View full-size slide

  136. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }
    WeekDay
    Date?

    View full-size slide

  137. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }
    Date
    WeekDay
    Date?

    View full-size slide

  138. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }
    Date

    View full-size slide

  139. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }
    Date date as Date

    View full-size slide

  140. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }
    Date date as Date
    throw NullPointerException()

    View full-size slide

  141. null!!
    class Episode {
    var airdate: Date = null!!
    }
    fun getWeekDay(date: Date?): WeekDay {
    return date!!.getWeekDay()
    }
    Date

    View full-size slide

  142. null!!
    class Episode {
    var airdate: Date = null!!
    }

    View full-size slide

  143. null!!
    class Episode {
    var airdate: Date = null!!
    }

    View full-size slide

  144. null!!
    class Episode {
    var airdate: Date = throw KotlinNullPointerException()
    }

    View full-size slide

  145. null!!
    class Episode {
    lateinit var airdate: Date
    }

    View full-size slide

  146. Platform types in
    overrides
    How do I choose the right type when overriding Java methods?

    View full-size slide

  147. Platform types in overrides

    View full-size slide

  148. Platform types in overrides
    public interface OnClickListener {
    void onClick(Button button);
    }

    View full-size slide

  149. Platform types in overrides
    public interface OnClickListener {
    void onClick(Button button);
    }
    class KtListener : OnClickListener {
    override fun onClick(button: Button?): Unit {
    val name = button?.name ?: "Unknown button"
    println("Clicked ${name}")
    }
    }

    View full-size slide

  150. Platform types in overrides
    public interface OnClickListener {
    void onClick(Button button);
    }
    class KtListener : OnClickListener {
    override fun onClick(button: Button?): Unit {
    val name = button?.name ?: "Unknown button"
    println("Clicked ${name}")
    }
    }

    View full-size slide

  151. Platform types in overrides
    public interface OnClickListener {
    void onClick(Button button);
    }
    class KtListener : OnClickListener {
    override fun onClick(button: Button?): Unit {
    val name = button?.name ?: "Unknown button"
    println("Clicked ${name}")
    }
    }

    View full-size slide

  152. Platform types in overrides
    public interface OnClickListener {
    void onClick(Button button);
    }
    class KtListener : OnClickListener {
    override fun onClick(button: Button): Unit {
    val name = button.name
    println("Clicked ${name}")
    }
    }

    View full-size slide

  153. Import aliases
    What can I do about imports with conflicting names?

    View full-size slide

  154. Import aliases

    View full-size slide

  155. Import aliases
    package com.example.code
    fun indent(str: String)
    : String { ... }
    package com.example.square
    fun indent(str: String)
    : String { ... }

    View full-size slide

  156. Import aliases
    package com.example.code
    fun indent(str: String)
    : String { ... }
    package com.example.square
    fun indent(str: String)
    : String { ... }
    import com.example.code.indent
    indent("hello")
    com.example.square.indent("world")

    View full-size slide

  157. Import aliases
    package com.example.code
    fun String.indent()
    : String { ... }
    package com.example.square
    fun String.indent()
    : String { ... }

    View full-size slide

  158. Import aliases
    package com.example.code
    fun String.indent()
    : String { ... }
    package com.example.square
    fun String.indent()
    : String { ... }
    import com.example.code.indent
    "hello".indent()
    "world".indent()

    View full-size slide

  159. Import aliases
    package com.example.code
    fun String.indent()
    : String { ... }
    package com.example.square
    fun String.indent()
    : String { ... }
    import com.example.code.indent as indent4
    import com.example.square.indent as indent2
    "hello".indent4()
    "world".indent2()

    View full-size slide

  160. Import aliases
    import java.util.Date as UtilDate
    import java.sql.Date as SqlDate
    fun matches(date1: UtilDate, date2: SqlDate): Boolean {
    ...
    }

    View full-size slide

  161. Kotlin vs Java for
    Android development
    Please help I just started

    View full-size slide

  162. Kotlin vs Java for Android development

    View full-size slide

  163. Kotlin vs Java for Android development

    View full-size slide

  164. Recap
    • 1. Array vs IntArray
    • 2. Iterable vs Sequence
    • 3. Iteration
    • 4. SAM conversions
    • 5. Replacing statics
    • 6. Smart casts on mutable properties
    • 7. null!!
    • 8. Platform types in overrides
    • 9. Import aliases
    • 10. Kotlin vs Java for Android development

    View full-size slide

  165. References
    • Top 10 Kotlin Stack Overflow questions, article series
     https://zsmb.co/top-10-kotlin-stack-overflow-questions-1/
    • Kotlin tag on Stack Overflow
     https://stackoverflow.com/questions/tagged/kotlin

    View full-size slide

  166. Photo:
    Alexey Sergeev
    Questions?
    zsmb.co zsmb13
    [email protected]

    View full-size slide