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
r
It makes sense for
toBoolean()
because a
null
input is mapped to a different output (
false
in this case). If, however, a
null
input is always a
null
output, there's no reason to accept
null
inputs as the language already has a built in way to handle that in the general case (i.e.
?.
).
5
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