elye
11/28/2017, 10:04 PMRuckus
11/28/2017, 10:19 PMWhen takeIf returns null, it shouldn’t be allow to get chained directly into any function without the ? check.Why do you think this? What if you have a function you specifically want to call that takes a nullable receiver?
elye
11/28/2017, 10:27 PM?.
tmg
11/28/2017, 10:31 PMRuckus
11/28/2017, 10:35 PMMaybe
wouldn't solve the problem. The root issue here is that apply
can take a nullable receiver, and you need to watch out for it.tmg
11/28/2017, 10:36 PMRuckus
11/28/2017, 10:37 PM.apply
can take a nullable receiver. If you don't want to, you specify ?.apply
. In this case Maybe
has no benefit over a nullable type.tmg
11/28/2017, 10:37 PMnull
, otherwise it cause problems with nullable tupesRuckus
11/28/2017, 10:38 PMtmg
11/28/2017, 10:38 PMRuckus
11/28/2017, 10:40 PM?.takeIf
tmg
11/28/2017, 10:42 PMtakeIf
definitation that I'm contestingRuckus
11/28/2017, 10:43 PMtmg
11/28/2017, 10:43 PMRuckus
11/28/2017, 10:44 PMtmg
11/28/2017, 10:45 PM?
seems to work like a monad maybe, I need more reading and proof. then for that example in the linked article, shouldn't the doThis()
method have the type object?
?doThis()
being a method of some nullable class?gildor
11/29/2017, 3:44 AM?.
operator at all, it’s wrong usage of takeIf
function in this article. Because looks like doThis
is just a function or method in context.
You never should use takeIf like that.
Take if make sense if you want to use someObject itself (pass it somewhere or call method) it’s not a replacement for general if conditiongeatmo
11/29/2017, 9:15 AMapply
on a variable that's null
, unless you try to access the variable in the passed lambda in a non-null
fashion.. even though apply
is defined on T
instead of T?
gildor
11/29/2017, 9:31 AMT
means that type can be nullalbe or non-nullable. If you want to define generic that allows only non-nullable types you should define it as T : Any
geatmo
11/29/2017, 9:31 AMgildor
11/29/2017, 9:32 AMpublic inline fun <T : Any> T.nonNullApply(block: T.() -> Unit): T {
block()
return this
}
tmg
11/29/2017, 9:33 AMRuckus
11/29/2017, 4:21 PMtakeIf
and apply
. The author claims the new block is somehow an improvement, but if you ask me it's way less readable than
if (someObject != null && status) doThis()
Using different syntax is only an improvement if it actually improves.