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

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)
主站蜘蛛池模板: 郎溪县| 遵义市| 长汀县| 鸡西市| 巨野县| 博爱县| 临海市| 龙泉市| 石柱| 磴口县| 大田县| 西丰县| 蒙自县| 福安市| 五常市| 阳泉市| 确山县| 哈尔滨市| 安乡县| 砀山县| 呼玛县| 绥德县| 井陉县| 全州县| 外汇| 襄樊市| 内丘县| 海安县| 车致| 新邵县| 永昌县| 来宾市| 阿城市| 绥宁县| 遵义县| 南郑县| 中宁县| 武功县| 平昌县| 宽甸| 兰溪市|