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

Member extension functions

Extension functions are usually declared at the top level, but we can define them inside classes as members. This may be used if we want to limit the scope of an extension:

    class Mappings { 
      private val map = hashMapOf<Int, String>() 
      private fun String.stringAdd(): Unit { 
        map.put(hashCode(), this) 
      } 
 
      fun add(str: String): Unit = str.stringAdd() 
    }

In this example, we have defined an extension function that adds a string to hashmap. The second function just invokes this extension function. This roundabout way of adding to hashmap indicates how receivers work in member extension functions.

The hashCode function is defined on Any, and so it exists on the Mappings and String classes through inheritance. When hashCode is invoked in the extension function, there are two possible functions in scope that could be used. The first function in the Mappings instance is called the dispatch receiver. The second function on the string instance is called the extension receiver.

When we have this kind of name shadowing, the compiler defaults to the extension receiver. In the previous example, the hash code used will be the hash code of the string instance. To use the dispatch receiver, we must use a qualified this:

    class Mappings { 
      private val map = hashMapOf<Int, String>() 
 
      private fun String.stringAdd(): Unit { 
        map.put(this@Mappings.hashCode(), this) 
      } 
      fun add(str: String): Unit = str.stringAdd() 
     } 

In this second example, the hashCode function will be invoked on the Mappings instance.

主站蜘蛛池模板: 漳平市| 台山市| 巧家县| 黑水县| 东乡县| 陇南市| 太康县| 黄大仙区| 蓬溪县| 柳河县| 北碚区| 岳普湖县| 枣庄市| 宣汉县| 临漳县| 仪征市| 新巴尔虎左旗| 英超| 文安县| 桓台县| 重庆市| 石泉县| 加查县| 香港| 东阿县| 弥渡县| 油尖旺区| 清苑县| 城固县| 石棉县| 台安县| 苍山县| 仙居县| 吉木萨尔县| 铁力市| 新竹市| 封丘县| 吉林市| 隆德县| 许昌县| 合作市|