Hi all, I don't know if this is the right channel,...
# language-proposals
d
Hi all, I don't know if this is the right channel, but I encountered the function
toDoubleOrNull()
(similar functions exist for other numeric types). This extension function is defined on
String
instead of
String?
Would it make sense to define this function on
String?
instead so a null-valued String just returns null? I noticed that
toBoolean()
is defined on
String?
as well.
r
I prefer having to add the null dereferencing ? - it's one character and makes it clear I know the value could be null.
d
I guess it's a matter of personal preference then, but since toBoolean has it, I thought that there would be a valid precedent, but maybe toBoolean is simpler since only "true" (case insensitive) yields true and everything else (including null) yields false
e
I also think
.toBoolean()
isn't a great example anyway; its behavior is a carry-over from Java's
Boolean.parseBoolean
, but unless you need to be precisely compatible with that, I would recommend
.toBooleanStrict()
or
.toBooleanStrictOrNull()
instead
2