```class Foo(val bar: Boolean) var foo: Foo? // i...
# announcements
m
Copy code
class Foo(val bar: Boolean)

var foo: Foo? // initialized somewhere in the code

fun test(): Boolean {
    return foo != null && !foo!!.bar // doesn't look nice
}
How can I make the expression in test() method nicer assuming I can't make foo non nullable?
the only thing I could come up with is this:
!(foo?.bar ?: true)
but I'm not sure I like it more
d
foo?.bar == false
m
nice, thanks!
e
also possible,
Copy code
fun test(): Boolean {
    val foo = foo
    return foo != null && !foo.bar
}
e
@ephemient I'm assuming that's not possible because
foo
is a
var
You can do:
Copy code
fun test() = foo?.let { !it.bar } == true
e
@eygraber it works if you re-bind to a val, which is what I'm doing there with
val foo = foo
. yours is pretty much the same as the Take's suggestion.
another way of rebinding is
Copy code
foo.let { it != null && !it.bar }
if you use the name
foo
again, e.g.
.let { foo ->
, then it's entirely equivalent to my suggestion
e
Oh I skimmed over that line. You're right.