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

Talk about "Nothing"

D5704d6f5845fd3821b2fa3eeffbc57a?s=47 Kelvin Ko
January 25, 2020

Talk about "Nothing"

Talk about "Nothing" in Kotlin愛好会 x GDG Kyushu


Kelvin Ko

January 25, 2020

More Decks by Kelvin Ko

Other Decks in Programming


  1. Talk about “Nothing” In Kotlin Kelvin Ko @ko_kelvin

  2. What is “Nothing”? • From Kotlin Doc: “ଘࡏ͠ͳ͍஋”

  3. What actually it is? • A Class with private construcor

    • Cannot be instaniated
  4. What is “Nothing”? • “A value that never exist”, sounds

    easy to understand • But • What is the different between Null and Nothing? “ଘࡏ͠ͳ͍஋”
  5. Kotlin type system • Nothing is a “Bottom Type” •

    SubType of any other Types (extends all other types) • In opposite, Any is Super class of any other class (World without Null) (World with Null) Image Reference: https://medium.com/@m.sandovalcalvo/kotlin-type-system-unveiling-the-mystery-50613f0db893
  6. So, “Nothing” is • Used as a Type, no instance

    • Bottom Type (Sub Type of any other Type)
  7. Why we need this?

  8. Use cases 1. Indicate function never return 2. As a

    “empty” Type in Generic
  9. Example of Function that never return • listenIncomingConnection will run

    forever (May not be a good way to implement like this, just simple example)
  10. Use case • Adding Nothing, IDE will know the println

    is unreachable This look correct, but actually this line will never run (Video example is uploaded here: https://youtu.be/fjECMd1H-hI)
  11. Use case 2: Exception • After throw Exception, program will

    end • It is more accurate to use “Nothing” as return type to indicate it never return Assume we would Load some data from Database, and throw Exception if it return null (Video example is uploaded here: https://youtu.be/HG210SN4_iI)
  12. • So IDE will warn you the code after TODO

    is unreachable Stdlib’s TODO() • Actually TODO() is implemented like this
  13. Why we need a class that is Sub Type of

    any other classes? Bottom Type Image Reference: https://medium.com/@m.sandovalcalvo/kotlin-type-system-unveiling-the-mystery-50613f0db893
  14. 2. Usage in Generic • Example: A Linked List implementation

    LinkedList Example Reference: https://www.codementor.io/@kotlin_academy/kotlin-s-nothing-its-usefulness-in-generics-j4wcu5taa
  15. Linked List implementation • To represent Empty List, add a

    single instance object • Won’t compile • Kotlin Object cannot have type parameters.
  16. Linked List implementation • How about remove Type parameter ?

    • Won’t compile • The reference to T is unresolved. We must specific a type for T
  17. Linked List implementation • Use “Nothing” as Type • It

    work because Nothing is subtype of any other Type
  18. Inside Kotlin Stdlib • emptyList() implementation: •

  19. Conclusion • Nothing is • Subtype of any other type

    (Bottom Type) • Useful in Generic • Indicate function that Never return • Infinite loop • Throw exception
  20. I hope you learn Nothing today Thank you for listening