Consider removing String.toLocalDate()
, String.toInstant()
, etc.
#339
Labels
formatters
Related to parsing and formatting
Right now, we have two ways to parse something:
With the introduction of datetime formatting (#251), we'll get other ways to do exactly the same:
and some others.
The form
"2024-01-23".toLocalDate()
looks a bit out-of-place. Parsing a string into aLocalDate
is a way to construct aLocalDate
(and so fits well into the companion object ofLocalDate
), not a property ofString
. We do have"123".toInt()
,"123".toDouble()
, and other primitive types already. We don't have"1m".toDuration()
forkotlin.time.Duration
.In terms of separation of responsibilities,
String.toLocalDate()
looks strange, but it can be practical when chaining calls. Of the three options below, usingtoLocalDate
looks the least busy:The problem is that it's tough to think of cases when such code would be written. Typically, when you accept some unstructured data, it should be parsed and validated first and only then used, so
birthDate
should probably already be aLocalDate
and not need any conversion. ForString.toInt()
, it's clear when it could be useful: for example, when just a single number is being parsed, which is fairly common. It's more difficult to think of dates that are not part of some larger data structure.The text was updated successfully, but these errors were encountered: