https://kotlinlang.org logo
Title
d

dekans

08/02/2017, 2:44 PM
maybe
iAmAVar?.let{ it+"XXX" } ?: "XXX"
h

horse_badorties

08/02/2017, 2:51 PM
iAmAVar
is a
Foo
, not a
String
though...
would be
iAmAVar?.let{ it.name+"XXX" } ?: "XXX"
, right?
d

dekans

08/02/2017, 2:57 PM
sure
👍 1
d

diesieben07

08/02/2017, 3:02 PM
imho a bit of an overuse of let here. But... it's opinion.
👍 1
d

dekans

08/02/2017, 3:13 PM
let is here for nullity-check, that's exactly his goal
d

diesieben07

08/02/2017, 3:14 PM
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:
val thing = myExpensiveMethod()
// do stuff with thing
Can be rewritten as:
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

dekans

08/02/2017, 3:37 PM
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