dekans
08/02/2017, 2:44 PMiAmAVar?.let{ it+"XXX" } ?: "XXX"
horse_badorties
08/02/2017, 2:51 PMiAmAVar
is a Foo
, not a String
though...horse_badorties
08/02/2017, 2:53 PMiAmAVar?.let{ it.name+"XXX" } ?: "XXX"
, right?dekans
08/02/2017, 2:57 PMdiesieben07
08/02/2017, 3:02 PMdekans
08/02/2017, 3:13 PMdiesieben07
08/02/2017, 3:14 PMlet
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
}
diesieben07
08/02/2017, 3:15 PMdekans
08/02/2017, 3:37 PMdekans
08/02/2017, 3:39 PMwith
for this use case.
/me back in kotlin docs to find out best practice