`thing?.takeIf { anotherCondition }?.run(::doSomet...
# announcements
k
thing?.takeIf { anotherCondition }?.run(::doSomething)
I believe would work
👍 4
e
thing?.takeIf { anotherCondition }?.doSomething()
should work, too
👍 2
k
ah of course
c
Actually I over simplified my problem. Here is what it looks like
Copy code
if (mCachedTasks != null && !mCacheIsDirty) {
    callback.onTasksLoaded(ArrayList(mCachedTasks!!.values))
    return
}
so the
run
version seems more applicable to me
Copy code
mCachedTasks
    .takeIf { !mCacheIsDirty }
    ?.run { 
        callback.onTasksLoaded(ArrayList(mCachedTasks?.values))
        return
    }
z
For null checking I do: thing?.let { ... it.something()...}. Is that ok? Or is takeIf better solution?
k
.takeIf
returns the object if the condition is true, or
null
if it was false. So it's a good way to filter out on some condition other than null. For example, you might not want to do
employee?.foo()
if the employee is non-null but invalid, so you can do
employee?.takeIf { it.isValid }?.foo()
m
Who not simply assign
var
to local
val
and leave
if
?
e
DRY - Don't repeat youself. When you operate on one thing it is eaiser to follow logic with
it
as opposed to repeating the name of this thing over and over.
m
DRY is about business rules, not low level code, but that's not for Slack 😉