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

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.

主站蜘蛛池模板: 文山县| 突泉县| 津市市| 平果县| 盐城市| 佛山市| 平利县| 鄂托克旗| 烟台市| 贡觉县| 峨边| 德清县| 吴江市| 新干县| 渝北区| 旬邑县| 哈巴河县| 徐州市| 磐安县| 龙口市| 阿勒泰市| 娄烦县| 博客| 白沙| 仁怀市| 乐陵市| 陕西省| 博爱县| 大新县| 泰宁县| 宜川县| 衡阳县| 泗阳县| 若羌县| 汶上县| 城步| 大方县| 鸡泽县| 阆中市| 泽普县| 河北区|