February 2018
Intermediate to advanced
350 pages
7h 35m
English
We already covered object expressions, but there is more on objects. Objects are natural singletons (by natural, I mean to come as language features and not as behavior pattern implementations, as in other languages). A singleton is a type that has just one and only one instance and every object in Kotlin is a singleton. That opens a lot of interesting patterns (and also some bad practices). Objects as singletons are useful for coordinating actions across the system, but can also be dangerous if they are used to keep global state.
Object expressions don't need to extend any type:
fun main(args: Array<String>) { val expression = object { val property = "" fun method(): Int { println("from an object expressions") return 42 } } val ...
Read now
Unlock full access