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

There's more...

When working with lambdas, whenever we want to execute the code inside their body, we need to call the invoke() function on them or its equivalent, the () operator:

val callback: () -> Unit = { println("The job is done!") }
callback.invoke()
callback()

The preceding code is going to print the text twice:

"The job is done!"
"The job is done!"

There is also another clean way of passing functions as the parameters to other functions. We can do it using function references:

fun hideView(view: View): Unit  {
view.visibility = View.INVISIBLE
}

submitButton.setOnClickListener(::hideView)

The function references approach can be particularly useful for reusing the function implementation across the codebase.

主站蜘蛛池模板: 炉霍县| 遵义县| 黄陵县| 方山县| 建水县| 马尔康县| 额尔古纳市| 德州市| 旬邑县| 和平县| 大兴区| 阳西县| 内乡县| 正宁县| 瑞安市| 萍乡市| 应用必备| 甘谷县| 阿坝县| 宝应县| 罗田县| 遂平县| 康乐县| 双流县| 肃北| 忻州市| 雷波县| 囊谦县| 衡山县| 昌图县| 余干县| 济阳县| 五河县| 水富县| 博罗县| 诸城市| 五指山市| 灯塔市| 隆昌县| 四平市| 宣化县|