var age : Int? = 0 public val isAdult : Boolean? get() = age?.let { it >= 18 }
Another solution would be to use delegates:
var age : Int by Delegates.notNull() public val isAdult : Boolean get () = age >= 18
Therefore, if you try to determine age or check isAdult before age is assigned, you will get an exception instead of zero.
In any case, I believe that age = 0 is some kind of magic that one day can lead to problems (even to a problem).
source share