Hi all, just a quick question: The difference betw...
# announcements
s
Hi all, just a quick question: The difference between this:
Copy code
val Foo = Bar()
and
Copy code
val Foo
   get() = Bar()
is that when using getter, the evaulation happens every time when accessing
Foo
, right?
🙏 1
👌 6
r
Yeah, exactly. In the first line,
Bar()
will be called immediately when the surrounding object is first created, and you're stuck with that particular instance of
Bar
for the rest of the surrounding object's lifetime. In the second line, a new instance of
Bar
is created everytime someone accesses
Foo
, but no permanent reference to any
Bar
will be kept around (at least inside this object).
s
Thanks for the clarification. Then the latter is exactly what I'll need 🙂
c
in the second variant your getter has a side effect. so maybe you want to use a real method instead of a getter
r
It's only a side effect if
Bar()
has a side effect. If
Bar
is just a simple data class, nothing happening there, so I'd say it's not that problematic.
s
Actually my use case might have side effects, so I think I'll use regular functions instead. Still good to know though, thanks 🙂