https://kotlinlang.org logo
#announcements
Title
# announcements
k

kevinmost

04/27/2017, 4:16 PM
thing?.takeIf { anotherCondition }?.run(::doSomething)
I believe would work
👍 4
e

elizarov

04/27/2017, 4:19 PM
thing?.takeIf { anotherCondition }?.doSomething()
should work, too
👍 2
k

kevinmost

04/27/2017, 4:21 PM
ah of course
c

curioustechizen

04/27/2017, 4:28 PM
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

zokipirlo

04/27/2017, 5:31 PM
For null checking I do: thing?.let { ... it.something()...}. Is that ok? Or is takeIf better solution?
k

kevinmost

04/27/2017, 5:33 PM
.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

mg6maciej

04/27/2017, 5:43 PM
Who not simply assign
var
to local
val
and leave
if
?
e

elizarov

04/27/2017, 5:47 PM
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

mg6maciej

04/27/2017, 6:09 PM
DRY is about business rules, not low level code, but that's not for Slack 😉