Mutability
suggest changeEnums can be mutable, this is another way to obtain a singleton behavior:
enum class Planet(var population: Int = 0) {
EARTH(7 * 100000000),
MARS();
override fun toString() = "$name[population=$population]"
}
println(Planet.MARS) // MARS[population=0]
Planet.MARS.population = 3
println(Planet.MARS) // MARS[population=3]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents