Typical Kotlin (Kotlin Budapest User Group meetup - September)

4047c64e3a1e2f81addd4ba675ddc451?s=47 Marton Braun
September 20, 2018

Typical Kotlin (Kotlin Budapest User Group meetup - September)

The talk covers some of the basic built-in types of Kotlin (Any, Unit, Nothing), and how we interact with these types - whether we know it or not - when using basic constructs (the Elvis operator, return statements, null itself) of the language.

4047c64e3a1e2f81addd4ba675ddc451?s=128

Marton Braun

September 20, 2018
Tweet

Transcript

  1. 2.
  2. 6.

    Any The root of the hierarchy public open class Any

    { public open operator fun equals(other: Any?): Boolean public open fun hashCode(): Int public open fun toString(): String }
  3. 8.
  4. 9.
  5. 11.
  6. 12.
  7. 13.
  8. 14.
  9. 15.
  10. 16.
  11. 17.
  12. 18.
  13. 19.
  14. 21.

    val noResult: Task< > Unit Unit The type with only

    one value fun () { // Empty! } empty() { // Empty! } Task< > noResult; Unit void Void : empty
  15. 24.

    Nothing A value that never exists fun loopy(): Nothing {

    while (true) { println("Loop!") } } fun exceptional(): Nothing { throw IllegalStateException() }
  16. 27.

    Nothing The bottom type fun processData(data: List<String>) { // Use

    data } fun main(args: Array<String>) { val data: Nothing = loopy() processData(data) }
  17. 28.

    Nothing Elvis’ best friend fun calculate(someParam: Int?) { val x

    = someParam ?: exceptional() val y = x * 2 println(y) }
  18. 30.
  19. 31.
  20. 32.
  21. 35.

    * 2 someParam ?: return fun calculate(someParam: Int?) { val

    y = println(y) } Nothing Elvis’ best friend x val x =
  22. 36.

    someParam ?: return * 2 fun calculate(someParam: Int?) { val

    y = println(y) } someParam Nothing Elvis’ best friend
  23. 39.

    Nothing? Nothing. var x = null x = "foo" Type

    mismatch. Required: Nothing? Found: String
  24. 41.

    Resources • Illustration style credit  http://natpryce.com/articles/000818.html • More about

    Nothing  http://oneeyedmen.com/nothing-can-save-us.html  https://blog.kotlin-academy.com/kotlins-nothing-its- usefulness-in-generics-5076a6a457f7