https://kotlinlang.org logo
Title
c

cedric

06/12/2017, 4:33 PM
@tulio A few thoughts here: http://beust.com/weblog/2016/01/14/a-close-look-at-kotlins-let/ tldr: I tend to stick to
if
d

diego-gomez-olvera

06/12/2017, 4:36 PM
cedric:
let
is good for
var
, which must generally be avoided but it is needed sometimes.
c

cedric

06/12/2017, 4:38 PM
Can you elaborate?
d

diego-gomez-olvera

06/12/2017, 4:40 PM
var
can change, and
let
extracts a local constant value (
it
). I just saw in the comments the one from ‘Justin Lee’, which gives the same reason
c

cedric

06/12/2017, 4:40 PM
Ah yes indeed
t

trevjones

06/12/2017, 4:42 PM
if you want to avoid
it
and avoid renaming can’t you just shadow it?
thing?.let { thing -> thing.doStuff() }
c

cedric

06/12/2017, 4:43 PM
Yes shadowing works too. Usually not recommended but it's basically the exact same variable, just transtyped from
T?
to
T
, so purists might give this infraction a pass
t

trevjones

06/12/2017, 4:45 PM
I do agree with what you are saying about if without else branch is typically a smell. it usually implies a stateful soupy mess imo
c

cedric

06/12/2017, 4:47 PM
I don't connect the abence of
else
with statefulness (nor that statefulness is necessarily bad) but I do agree that the absence of a
let
is suspicious in my blog post
r

roberto.guerra

06/12/2017, 5:14 PM
I tend to stick with
if
also. I find it much more readable in the long run when I go back to read the code.
c

crpr

06/12/2017, 6:02 PM
Swift has a if let that allows for the else. Maybe in a new kotlin version
d

Daniel

06/12/2017, 7:19 PM
nullable?.let { doSomethingNotNull(it) } ?: doSomesingElse()
Is the construct for the else branch in Kotlin if i am not mistaken