- Android Development with Kotlin
- Marcin Moskala Igor Wojda
- 147字
- 2021-07-02 18:48:35
The let function
Another tool to deal with nullable variables is the let function. This is actually not an operator, nor a language special construct. It is a function defined in the Kotlin standard library. Let's see the syntax of let combined with the safe call operator:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) savedInstanceState?.let { println(it.getBoolean("isLocked")) // 1 } }
- savedInstanceState inside the let function can be accessed using the it variable.
As mentioned before, the right-hand expression of the safe call operator will be only evaluated if the left-hand side is not null. In this case, the right-hand side is a let function that takes another function (lambda) as a parameter. Code defined in the block after let will be executed if savedInstanceState is not null. We will learn more about it and how to define such functions in Chapter 7, Extension Functions and Properties.
推薦閱讀
- Spring 5企業級開發實戰
- Vue.js 3.0源碼解析(微課視頻版)
- Mastering PHP Design Patterns
- Modern JavaScript Applications
- Scientific Computing with Scala
- Java Web程序設計任務教程
- R語言與網絡輿情處理
- Python3.5從零開始學
- Python從入門到精通
- Android應用開發深入學習實錄
- OpenCV with Python Blueprints
- Spring Boot從入門到實戰
- Practical Linux Security Cookbook
- 中小企業網站建設與管理(靜態篇)
- Python量子計算實踐:基于Qiskit和IBM Quantum Experience平臺