Slide 1

Slide 1 text

Talk about “Nothing” In Kotlin Kelvin Ko @ko_kelvin

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

What actually it is? • A Class with private construcor • Cannot be instaniated

Slide 4

Slide 4 text

What is “Nothing”? • “A value that never exist”, sounds easy to understand • But • What is the different between Null and Nothing? “ଘࡏ͠ͳ͍஋”

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

So, “Nothing” is • Used as a Type, no instance • Bottom Type (Sub Type of any other Type)

Slide 7

Slide 7 text

Why we need this?

Slide 8

Slide 8 text

Use cases 1. Indicate function never return 2. As a “empty” Type in Generic

Slide 9

Slide 9 text

Example of Function that never return • listenIncomingConnection will run forever (May not be a good way to implement like this, just simple example)

Slide 10

Slide 10 text

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)

Slide 11

Slide 11 text

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)

Slide 12

Slide 12 text

• So IDE will warn you the code after TODO is unreachable Stdlib’s TODO() • Actually TODO() is implemented like this

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Linked List implementation • To represent Empty List, add a single instance object • Won’t compile • Kotlin Object cannot have type parameters.

Slide 16

Slide 16 text

Linked List implementation • How about remove Type parameter ? • Won’t compile • The reference to T is unresolved. We must specific a type for T

Slide 17

Slide 17 text

Linked List implementation • Use “Nothing” as Type • It work because Nothing is subtype of any other Type

Slide 18

Slide 18 text

Inside Kotlin Stdlib • emptyList() implementation: •

Slide 19

Slide 19 text

Conclusion • Nothing is • Subtype of any other type (Bottom Type) • Useful in Generic • Indicate function that Never return • Infinite loop • Throw exception

Slide 20

Slide 20 text

I hope you learn Nothing today Thank you for listening