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

Typical Kotlin (Kotlin Budapest User Group meetup - September)

Márton 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.

Márton Braun

September 20, 2018
Tweet

More Decks by Márton Braun

Other Decks in Programming

Transcript

  1. 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 }
  2. val noResult: Task< > Unit Unit The type with only

    one value fun () { // Empty! } empty() { // Empty! } Task< > noResult; Unit void Void : empty
  3. Nothing A value that never exists fun loopy(): Nothing {

    while (true) { println("Loop!") } } fun exceptional(): Nothing { throw IllegalStateException() }
  4. Nothing The bottom type fun processData(data: List<String>) { // Use

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

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

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

    y = println(y) } someParam Nothing Elvis’ best friend
  8. Nothing? Nothing. var x = null x = "foo" Type

    mismatch. Required: Nothing? Found: String
  9. 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