JP
05/23/2020, 2:20 PMcheckNotNull(x).also {
// do something like..
it.prop = y
// ...
}
2.
x?.apply {
// do something like..
it.prop = y
// ...
} ?: throw IllegalStateException()
Bornyls Deen
05/23/2020, 2:46 PMx
being non-null.
I'd probably go with either
requireNotNull(x)
x.prop = y
or
requireNotNull(x).apply {
prop = y
}
depending on whether or not the scoping/return value of apply
were required in your actual use case.Bornyls Deen
05/23/2020, 2:48 PMalso
for sneaking things with no side-effects like log statements into call chains.Bornyls Deen
05/23/2020, 2:56 PMrequireNotNull
over checkNotNull
because I think the name better reflects the consequences but it's worth noting that the exceptions that are thrown are different.JP
05/23/2020, 2:56 PMrequireNotNull
and checkNotNull
(also between require
and check
) only something conventional, such that one is used for checking function arguments, and one is used for checking other program states?Bornyls Deen
05/23/2020, 3:11 PMcheckNotNull
but use requireNotNull
mostly for things like reading from config files or validating POST requests. If my function required a non-null type I would just make that clear in the signature if possible 🙂Orhan Tozan
05/23/2020, 7:02 PM