- 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.
推薦閱讀
- ExtGWT Rich Internet Application Cookbook
- Advanced Machine Learning with Python
- 在最好的年紀學Python:小學生趣味編程
- Practical Internet of Things Security
- 少年輕松趣編程:用Scratch創作自己的小游戲
- Learning Elixir
- Windows Server 2012 Unified Remote Access Planning and Deployment
- TypeScript實戰指南
- 手把手教你學C語言
- Hands-On Natural Language Processing with Python
- 零基礎Java學習筆記
- Learning Material Design
- Managing Microsoft Hybrid Clouds
- OpenCV 3 Blueprints
- C++ System Programming Cookbook