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

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.

主站蜘蛛池模板: 铁岭市| 科技| 邵阳市| 师宗县| 尼木县| 石嘴山市| 苗栗市| 隆林| 五家渠市| 大悟县| 江都市| 辽宁省| 乌拉特中旗| 舟山市| 鞍山市| 麦盖提县| 东方市| 临江市| 白朗县| 什邡市| 崇明县| 广河县| 灵璧县| 贵德县| 城市| 白玉县| 通榆县| 浦江县| 湘阴县| 阳山县| 临夏县| 青河县| 东光县| 连云港市| 白玉县| 西平县| 平利县| 彰武县| 临西县| 赫章县| 甘谷县|