官术网_书友最值得收藏!

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)
主站蜘蛛池模板: 烟台市| 东源县| 黄山市| 普安县| 龙州县| 沁源县| 华坪县| 丘北县| 成武县| 铁岭市| 和林格尔县| 六安市| 宿州市| 东乌| 永靖县| 麦盖提县| 孝感市| 陆河县| 保德县| 通化市| 深州市| 那曲县| 天长市| 滦平县| 益阳市| 二手房| 大余县| 旌德县| 民勤县| 容城县| 宁城县| 枣庄市| 大方县| 会宁县| 鄂伦春自治旗| 淅川县| 上思县| 衡阳市| 日照市| 德清县| 襄城县|