- Functional Kotlin
- Mario Arias Rivu Chakraborty
- 139字
- 2021-06-24 19:15:24
The Nothing type
Nothing is the type that sits at the bottom of the entire Kotlin hierarchy. Nothing extends all Kotlin types, including Nothing?.
But, why do we need a Nothing and Nothing? types?
Nothing represents an expression that can't be executed (basically throwing exceptions):
val result: String = nullableCupcake?.eat() ?: throw RuntimeException() // equivalent to nullableCupcake!!.eat()
On one hand of the Elvis operator, we have a String. On the other hand, we have Nothing. Because the common type between String and Nothing is String (instead of Any), the value result is a String.
Nothing also has a special meaning for the compiler. Once a Nothing type is returned on an expression, the lines after that are marked as unreachable.
Nothing? is the type of a null value:
val x: Nothing? = null
val nullsList: List<Nothing?> = listOf(null)
推薦閱讀
- C程序設計簡明教程(第二版)
- Java EE框架整合開發(fā)入門到實戰(zhàn):Spring+Spring MVC+MyBatis(微課版)
- 無代碼編程:用云表搭建企業(yè)數(shù)字化管理平臺
- Xcode 7 Essentials(Second Edition)
- Unity Virtual Reality Projects
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊)
- Scratch真好玩:教小孩學編程
- 精通網(wǎng)絡視頻核心開發(fā)技術(shù)
- Python機器學習基礎(chǔ)教程
- Building Serverless Applications with Python
- Angular開發(fā)入門與實戰(zhàn)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- Image Processing with ImageJ
- Python無監(jiān)督學習
- FusionCharts Beginner’s Guide:The Official Guide for FusionCharts Suite