- Kotlin Standard Library Cookbook
- Samuel Urbanowicz
- 112字
- 2021-07-23 19:06:01
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.
推薦閱讀
- 黑客攻防從入門到精通(實戰秘笈版)
- Bootstrap Site Blueprints Volume II
- Apache Spark 2.x Machine Learning Cookbook
- Offer來了:Java面試核心知識點精講(原理篇)
- 實用防銹油配方與制備200例
- Interactive Applications Using Matplotlib
- PhoneGap:Beginner's Guide(Third Edition)
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- PHP編程基礎與實例教程
- Xamarin Cross-Platform Development Cookbook
- Laravel Design Patterns and Best Practices
- 現代C++語言核心特性解析
- Appcelerator Titanium Smartphone App Development Cookbook
- jQuery基礎教程(第4版)
- Go語言從入門到項目實戰(視頻版)