- Functional Kotlin
- Mario Arias Rivu Chakraborty
- 107字
- 2021-06-24 19:15:24
The Unit type
Kotlin doesn't have methods with void return (as Java or C do). Instead, a method (or, to be precise, an expression) could have a Unit type.
A Unit type means that the expression is called for its side effects, rather than its return. The classic example of a Unit expression is println(), a method invoked just for its side effects.
Unit, like any other Kotlin type, extends from Any and could be nullable. Unit? looks strange and unnecessary, but is needed to keep consistency with the type system. Having a consistent type system have several advantages, including better compilation times and tooling:
anyMachine.process(Unit)
推薦閱讀
- DevOps:軟件架構師行動指南
- 數據庫程序員面試筆試真題與解析
- 程序員面試白皮書
- Dynamics 365 Application Development
- Python爬蟲開發:從入門到實戰(微課版)
- C語言程序設計(第2版)
- 跟小海龜學Python
- Big Data Analytics
- PHP+MySQL網站開發項目式教程
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- Mastering Xamarin.Forms(Second Edition)
- Java SE實踐教程
- Java Web應用開發項目教程
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計
- 深入理解BootLoader