- 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.
推薦閱讀
- Learn TypeScript 3 by Building Web Applications
- Objective-C Memory Management Essentials
- Visual Basic編程:從基礎到實踐(第2版)
- CentOS 7 Server Deployment Cookbook
- FFmpeg入門詳解:音視頻流媒體播放器原理及應用
- Easy Web Development with WaveMaker
- HTML5+CSS3網站設計基礎教程
- iOS應用逆向工程(第2版)
- RSpec Essentials
- Mastering Unity 2D Game Development(Second Edition)
- Lighttpd源碼分析
- Domain-Driven Design in PHP
- Unity 3D腳本編程:使用C#語言開發跨平臺游戲
- Visual Basic 6.0程序設計實驗教程
- Python機器學習算法與應用