- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 104字
- 2021-06-24 14:13:36
Extension functions on null values
Kotlin even supports extension functions on null values. In those situations, the this reference will contain the null value, and so, Any function that doesn't safely handle null references would throw a null pointer exception.
This functionality is how the equals function can be overloaded to provide safe usage to even null values:
fun Any?.safeEquals(other: Any?): Boolean { if (this == null && other == null) return true if (this == null) return false return this.equals(other) }
This function can be called from both null and non-null values, and the Kotlin compiler will handle both cases for you.
推薦閱讀
- Oracle從新手到高手
- SpringMVC+MyBatis快速開發(fā)與項目實戰(zhàn)
- Windows系統(tǒng)管理與服務配置
- PHP 7底層設計與源碼實現(xiàn)
- Oracle 12c中文版數據庫管理、應用與開發(fā)實踐教程 (清華電腦學堂)
- Apache Spark 2.x for Java Developers
- 從Java到Web程序設計教程
- Python算法詳解
- Java高并發(fā)核心編程(卷1):NIO、Netty、Redis、ZooKeeper
- QGIS 2 Cookbook
- Hadoop大數據分析技術
- 并行編程方法與優(yōu)化實踐
- Python青少年趣味編程
- 深入解析Java編譯器:源碼剖析與實例詳解
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐