- 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.
推薦閱讀
- C語(yǔ)言程序設(shè)計(jì)實(shí)踐教程(第2版)
- Node.js Design Patterns
- Android應(yīng)用程序開(kāi)發(fā)與典型案例
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)實(shí)踐教程
- 算法零基礎(chǔ)一本通(Python版)
- Learn to Create WordPress Themes by Building 5 Projects
- Mastering Python High Performance
- 實(shí)戰(zhàn)Java高并發(fā)程序設(shè)計(jì)(第3版)
- PHP+MySQL網(wǎng)站開(kāi)發(fā)項(xiàng)目式教程
- QTP自動(dòng)化測(cè)試進(jìn)階
- Python Data Analysis Cookbook
- 汽車(chē)人機(jī)交互界面整合設(shè)計(jì)
- Building Dynamics CRM 2015 Dashboards with Power BI
- Flask Web開(kāi)發(fā):基于Python的Web應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)(第2版)
- QlikView Unlocked