is there any difference in these two ``` val isEmp...
# android
s
is there any difference in these two
Copy code
val isEmpty: Boolean
    get() = this.size == 0

val isEmpty =  this.size==0
l
yes, in first case it will be getter method with
this.size == 0
body, in second you just initialize property
🙏 1
j
you beat me to it
p
Yep, when the size changes, the value returned from the second property won't get updated
l
In other words, the second one is an immutable property with a backing field that can't change while the first ont is a read only property that returns isEmpty according to the curreny value of size
a
In these cases - where you want to use the first one because you don't want the immutable property - is it better to use a function instead of a val/override get, or are they effectively the same?
I find myself using the first one a lot more because I like the style, and that's probably fine for all intents and purposes, right?
l
@adam-mcneilly Computed read-only properties are fine and idiomatic, in fact you can see a lot of them in the stdlib, especially in collections
1
👍 1