- Functional Kotlin
- Mario Arias Rivu Chakraborty
- 105字
- 2021-06-24 19:15:25
Destructuring methods
By convention, any instance of a class that has a series of methods named component1(), component2() and so on can be used in a destructuring declaration.
Kotlin will generate these methods for any data class:
val (prod: BakeryGood, price: Double, qty: Int) = mySecondItem
The prod value is initialized with the return of component1(), price with the return of component2() , and so on. Although the preceding example use explicit types, those aren't needed:
val (prod, price, qty) = mySecondItem
In some circumstances, not all values are needed. All unused values can be replaced by (_):
val (prod, _, qty) = mySecondItem
推薦閱讀
- Spring Boot 2實戰之旅
- 零基礎學Visual C++第3版
- Redis Applied Design Patterns
- PostgreSQL Cookbook
- PHP 7底層設計與源碼實現
- Practical Windows Forensics
- Gradle for Android
- Visual Basic程序設計
- Distributed Computing in Java 9
- Arduino機器人系統設計及開發
- MySQL 8從零開始學(視頻教學版)
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- Spring Web Services 2 Cookbook
- 一步一步學Spring Boot:微服務項目實戰(第2版)
- Learning Dynamics NAV Patterns