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

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.

主站蜘蛛池模板: 大兴区| 泰兴市| 镇沅| 韶山市| 胶南市| 屏南县| 休宁县| 梅河口市| 揭阳市| 平顶山市| 昌乐县| 融水| 平定县| 友谊县| 读书| 新和县| 资中县| 虎林市| 平凉市| 招远市| 启东市| 内江市| 铁力市| 蓝山县| 密云县| 夏邑县| 鄂托克前旗| 中西区| 长葛市| 黄陵县| 泰宁县| 衡阳市| 綦江县| 察雅县| 滁州市| 沽源县| 新巴尔虎左旗| 广东省| 阿坝县| 白朗县| 阜平县|