maybe `iAmAVar?.let{ it+"XXX" } ?: "XXX"`
# getting-started
d
maybe
iAmAVar?.let{ it+"XXX" } ?: "XXX"
h
iAmAVar
is a
Foo
, not a
String
though...
would be
iAmAVar?.let{ it.name+"XXX" } ?: "XXX"
, right?
d
sure
👍 1
d
imho a bit of an overuse of let here. But... it's opinion.
👍 1
d
let is here for nullity-check, that's exactly his goal
d
No, it's not really.
let
is here for things where you need want to access something exactly once but need that "captured" value multiple times. Example:
Copy code
val thing = myExpensiveMethod()
// do stuff with thing
Can be rewritten as:
Copy code
myExpensiveMethod().let { thing -> 
   // do stuff with thing
}
👍 2
But, like I said, it's opinion. I think your version is a lot less redable.
d
Hmm ok, was not how I understood this, thanks! I'm still a beginner 🙂
I use
with
for this use case. /me back in kotlin docs to find out best practice