Richard
08/22/2024, 4:08 PMval Boolean?.isTrue
get() = this == true
What is better for x: Boolean?
?
if(x == true) ...
Or
if(x.isTrue) ...
We also have extension values isFalse
, isTrueOrNull
, isFalseOrNull
.
What do you think about those?Joffrey
08/22/2024, 4:13 PM.
vs ?.
. I kinda assume the left side is non-nullable if I see a .
. This is why I tend not to use .orEmpty()
for collections, and prefer ?: emptyList()
.
Now I have to admit I find x == true
a bit confusing too (personally) for nullable booleans, but this is technically the recommended style AFAIR.
So overall I wouldn't lean strongly towards either of those choices. Instead, I'd rather favor an approach where we just don't have that many nullable booleans, and this is not needed.ephemient
08/22/2024, 4:22 PM.toString()
which is definitely more convenient than ?.toString() ?: "null"
ephemient
08/22/2024, 4:22 PMJoffrey
08/22/2024, 4:27 PMthere are other standard extensions on nullable types like .toString() which is definitely more convenient than ?.toString() ?: "null"Usually, string interpolation does its job without me having to write
toString()
explicitly. I do write .toString()
explicitly sometimes, but I don't recall ever doing that on a nullable value. I would add that I don't recall ever needing the "null"
value outside string interpolation.ephemient
08/22/2024, 4:34 PMit.toString()
over "$it"
when that's the only thing being stringified. .equals()
is tooephemient
08/22/2024, 4:36 PM==
operator being magicephemient
08/22/2024, 4:37 PM.isNullOrEmpty()
.isNullOrBlank()
.orEmpty()
are useful but not really critical imoRichard
08/22/2024, 4:43 PMephemient
08/22/2024, 4:44 PMfoo.isTrue.somethingElse()
than (foo == true).somethingElse()
but that's not something you do with Booleans anywayRichard
08/22/2024, 4:45 PM.not()
Richard
08/22/2024, 4:45 PMephemient
08/22/2024, 4:46 PM==
in some way… common enough that there might be a shortcut for it…Richard
08/22/2024, 4:47 PMCLOVIS
08/22/2024, 4:51 PMCLOVIS
08/22/2024, 4:51 PM