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. Typical Kotlin Braun Márton Szabolcs zsmb.co zsmb13 braun.marton@autsoft.hu

  2. Hello

  3. Hello class

  4. Hello class

  5. Hello class class List<T>

  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 }
  7. Any The root of the hierarchy

  8. None
  9. None
  10. val maybeString: String? = null val definitelyString = maybeString ?:

    "replacement"
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. Unit public object Unit { override fun toString() = "kotlin.Unit"

    } The type with only one value
  21. val noResult: Task< > Unit Unit The type with only

    one value fun () { // Empty! } empty() { // Empty! } Task< > noResult; Unit void Void : empty
  22. Unit Unit The type with only one value void Void

  23. Nothing A value that never exists public class Nothing private

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

    while (true) { println("Loop!") } } fun exceptional(): Nothing { throw IllegalStateException() }
  25. Nothing A value that never exists

  26. Nothing The bottom type

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

    data } fun main(args: Array<String>) { val data: Nothing = loopy() processData(data) }
  28. Nothing Elvis’ best friend fun calculate(someParam: Int?) { val x

    = someParam ?: exceptional() val y = x * 2 println(y) }
  29. Nothing Elvis’ best friend

  30. None
  31. None
  32. None
  33. exceptional() Nothing Elvis’ best friend fun calculate(someParam: Int?) { val

    x = someParam ?: val y = x * 2 println(y) }
  34. Nothing Elvis’ best friend throw IllegalArgumentException("!") fun calculate(someParam: Int?) {

    val x = someParam ?: val y = x * 2 println(y) }
  35. * 2 someParam ?: return fun calculate(someParam: Int?) { val

    y = println(y) } Nothing Elvis’ best friend x val x =
  36. someParam ?: return * 2 fun calculate(someParam: Int?) { val

    y = println(y) } someParam Nothing Elvis’ best friend
  37. Nothing? Nothing.

  38. Nothing? Nothing.

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

    mismatch. Required: Nothing? Found: String
  40. Nothing Edge cases fun oddity() { throw return return throw

    return }
  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
  42. Questions? zsmb.co zsmb13 braun.marton@autsoft.hu