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

Typical Kotlin (Kotlin Budapest User Group meetup - September)

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.

Marton Braun

September 20, 2018
Tweet

More Decks by Marton Braun

Other Decks in Programming

Transcript

  1. Typical Kotlin
    Braun Márton Szabolcs
    zsmb.co zsmb13
    [email protected]

    View Slide

  2. Hello

    View Slide

  3. Hello
    class

    View Slide

  4. Hello
    class

    View Slide

  5. Hello
    class
    class List

    View Slide

  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
    }

    View Slide

  7. Any
    The root of the hierarchy

    View Slide

  8. View Slide

  9. View Slide

  10. val maybeString: String? = null
    val definitelyString = maybeString ?: "replacement"

    View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. View Slide

  20. Unit
    public object Unit {
    override fun toString() = "kotlin.Unit"
    }
    The type with only one value

    View Slide

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

    View Slide

  22. Unit
    Unit
    The type with only one value
    void
    Void

    View Slide

  23. Nothing
    A value that never exists
    public class Nothing private constructor()

    View Slide

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

    View Slide

  25. Nothing
    A value that never exists

    View Slide

  26. Nothing
    The bottom type

    View Slide

  27. Nothing
    The bottom type
    fun processData(data: List) {
    // Use data
    }
    fun main(args: Array) {
    val data: Nothing = loopy()
    processData(data)
    }

    View Slide

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

    View Slide

  29. Nothing
    Elvis’ best friend

    View Slide

  30. View Slide

  31. View Slide

  32. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  37. Nothing?
    Nothing.

    View Slide

  38. Nothing?
    Nothing.

    View Slide

  39. Nothing?
    Nothing.
    var x = null
    x = "foo"
    Type mismatch.
    Required: Nothing?
    Found: String

    View Slide

  40. Nothing
    Edge cases
    fun oddity() {
    throw return return throw return
    }

    View Slide

  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

    View Slide

  42. Questions?
    zsmb.co zsmb13
    [email protected]

    View Slide