jmfayard
11/05/2019, 10:43 AM.let { ... }
all the time and don't really understand the need for the others.
I know there are multiple articles on when to use
let, also, with, apply, run, takeIf, ...
But which one did you find the most useful?Stephan Schroeder
11/05/2019, 10:50 AMmaxmello
11/05/2019, 10:52 AMjmfayard
11/05/2019, 10:54 AMwith(ConfigObject) { ... }
https://pl.kotl.in/4I3H2kfPt
https://dev.to/jmfayard/with-configobject-language-kotlin-issparkingjoy-ic4jmfayard
11/05/2019, 11:15 AMStephan Schroeder
11/05/2019, 11:20 AMtakeIf
seems a bit indulgent (/code golfing) to me https://medium.com/@elye.project/using-kotlin-takeif-or-takeunless-c9eeb7099c22jmfayard
11/05/2019, 11:31 AM?: error("Error")
or ?: return false
Vishnu Haridas
11/05/2019, 11:33 AM?.let { ... }
to perform something when the receiver is not null. Example,
val response = getResponse(url)
response?.data?.userId?.let {
PreferenceHelper.userId = it
}
streetsofboston
11/05/2019, 12:11 PMStephan Schroeder
11/05/2019, 12:55 PMalso
would be more idiomatic:
val response = getResponse(url)
response?.data?.userId?.also {
PreferenceHelper.userId = it
}
On the other hand assuming that PreferenceHelper.userId
is null
before you set it and therefore nullable, why not use a simple assignment?!
PreferenceHelper.userId = response?.data?.userId
Vishnu Haridas
11/05/2019, 1:02 PMuserId
is not null. Anyway, curious to know why you said .also
is more idiomatic than .let
🤔Stephan Schroeder
11/05/2019, 1:07 PMlet
returns the last expression it computes, which can mess up your code, if that’s not what you’re intending (because if-else
is also an expression and suddenly starts complaining that you need an else
as well). So if you’re not using the value returned by let
, it’s better to use also
(which returns Unit
and is therefore safe(r)).
I see let
mostly as the “single value version of `map`“.pavi2410
11/05/2019, 1:26 PMmaxmello
11/05/2019, 2:13 PMjmfayard
11/05/2019, 2:15 PMT.run { ... }
does differently than T.let { ... }
but I don't like its name.
I don't find it clear at all.
Anybody has a better name?louiscad
11/06/2019, 1:27 PMalso
, with
, let
and apply
.gildor
11/06/2019, 4:09 PMwith
, only when want to use member extension function
Also think that local variable and if check is better than let for null check. A lot more explicit and more readablegildor
11/06/2019, 4:12 PMrun
as analogue of map
, convert one type to another (or let, depends on use case)
takeIf is quite different than other scope funxtiobs, but like to use it in some cases where I want to validate object (often use for files or strings)louiscad
11/06/2019, 4:12 PMtakeIf
😅jmfayard
11/06/2019, 4:13 PM... yes but I don't find the nameas analogue ofT.run
map
run
clear at all that it is doing this
I would use it more if it had a better name
Not that I have a better suggestion mind you.maxmello
11/06/2019, 10:04 PMjmfayard
11/07/2019, 3:33 AMT.mapIt { }
(copy of let) and
T.mapThis { }
(copy of run)
?gildor
11/07/2019, 5:23 AMgildor
11/07/2019, 5:23 AMjmfayard
11/07/2019, 7:35 AM<T>.let
is map for a single value called it
- <T>.run { .. }
(not run { ... }
) is like let
but instead of the single value being it
, it is this
gildor
11/07/2019, 11:17 PMjmfayard
11/08/2019, 8:06 AM