In Kotlin, the uppermost type is called Any
. This is analogous to Java's object type. The Any
type defines the well-known toString
, hashCode
, and equals
methods. It also defines the extension methods apply
, let
, and to
, among others. These methods will be described in more detail in Chapter 5, Higher Order Functions and Functional Programming.
The Unit type is the equivalent of void in Java. Having a Unit type is common in a functional programming language, and the distinction between void and Unit is subtle. Void is not a type, but a special edge case that is used to indicate to the compiler that a function returns no value. Unit is a proper type, with a singleton instance, also referred to as Unit
or ()
. When a function is defined as a returning Unit, then it will return the singleton unit instance.
This results in greater soundness of the type system
as now all functions can be defined as having a return value, even if it's just the Unit type, and functions that have no arguments...