I read this <https://blog.mindorks.com/learn-kotli...
# announcements
a
I read this https://blog.mindorks.com/learn-kotlin-apply-vs-with But still can not understand the differences between
apply
and
with
. When should I use one over the other?
t
apply
returns the receiver, ie the object you called apply on. for instance
print("receiver".apply{ "something" })
prints
receiver
with
return the result of the operation, so
print(with("receiver") { "something" })
prints
something
r
I never ever used
with
, I’m not sure why it exists at all.
run
is equal or better in all cases
s
And also, when tempted to use
with
, it's usually better to refactor it into a private extension function.
l
I use
with
and never used
run
on production code.
e