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

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.

主站蜘蛛池模板: 青田县| 金坛市| 宜州市| 友谊县| 高淳县| 游戏| 大理市| 柘荣县| 太白县| 玉环县| 永川市| 盱眙县| 东光县| 大关县| 新乡县| 大港区| 石景山区| 延长县| 松原市| 临湘市| 嘉义县| 舞阳县| 肥城市| 临武县| 堆龙德庆县| 确山县| 临洮县| 三江| 辽宁省| 舟山市| 长兴县| 东方市| 迁西县| 前郭尔| 叶城县| 朔州市| 齐齐哈尔市| 磐安县| 尼勒克县| 赤峰市| 台江县|