What does `if (current is Set<*> && ...
# getting-started
k
What does
if (current is Set<*> && ...)
even do? Check whether it isn't null?
b
Correct
k
Then just do
!= null
, this is just obvustication ☺️.
b
why not just
!= null
, or better yet,
?.let {}
b
using
is
gives a smart cast
k
The null check also does that.
b
So instead do the following?
Copy code
filterStates[requested]?.takeIf { it.isNotEmpty() }?.let { additional ->
    filters[property]?.let { current ->
        if (current.isNotEmpty()) {
            filters[property] = current.plus(additional)
        } else {
            filters[property] = additional
        }
    }
}
k
I'm not a fan of nested `let`s, I'd go with
!= null
.
b
yea I guess if you're doing
if()
anyway, you can combine the
!=null
in there to make it a little shorter
b
Don’t forget that the
if
also contains additional conditions. Is the suggestion there to do
value!!.isNotEmpty()
instead of using the smart cast?
k
Smart casting works within boolean expressions too:
Copy code
fun foo(x: String?) {
	if(x != null && x.length == 2)
	    println("hey")
}
compiles.