mzgreen
05/25/2021, 11:53 AMclass 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?mzgreen
05/25/2021, 11:55 AM!(foo?.bar ?: true)
but I'm not sure I like it morediesieben07
05/25/2021, 11:56 AMfoo?.bar == false
mzgreen
05/25/2021, 11:59 AMephemient
05/25/2021, 5:47 PMfun test(): Boolean {
val foo = foo
return foo != null && !foo.bar
}
eygraber
05/26/2021, 3:47 AMfoo
is a var
You can do:
fun test() = foo?.let { !it.bar } == true
ephemient
05/26/2021, 4:27 AMval foo = foo
. yours is pretty much the same as the Take's suggestion.ephemient
05/26/2021, 4:28 AMfoo.let { it != null && !it.bar }
if you use the name foo
again, e.g. .let { foo ->
, then it's entirely equivalent to my suggestioneygraber
05/26/2021, 6:19 AM