an idea to the horrible replacement suggested for ...
# stdlib
e
an idea to the horrible replacement suggested for
capitalize()
: Gradle is using
capitalized()
j
It still doesn't solve the actual initial problem with
capitalize
. The point is that it's unclear what this does, and many devs have different expectations depending on their background. Does it capitalize every word? Just the first character? With which locale? Does it lowercase other letters of keep them uppercase if they are uppercase to start with?
e
just the first, default locale
j
That's your opinion of what should go behind the name
capitalize(d)
. The reason the Kotlin team deprecated this function is that there was no consensus on this, and therefore it was better to keep more specific helper functions, like
replaceFirstChar
and see how users combined them. Once this data is clearer, maybe a new helper will be provided in the stdlib, with a name adapted to the most common usages/use cases.
i
@elect Note that the suggested replacement is wordy only because it tries to preserve the behavior of the replaced function as accurately as possible. But in most cases something more compact as
replaceFirstChar { it.uppercase() }
may be sufficient.
j
r
If
uppercase()
is unambiguous than so should
uppercaseFirstChar()
? This and pascal/camel/snake case conversions come up quite often.
j
Actually
replaceFirstChar { it.uppercase() }
is probably also not what people should be using. Most probably people are looking for
replaceFirstChar { it.titlecase() }
instead (see the stackoverflow answer linked above), and thus introducing
uppercaseFirstChar
would probably be a bad idea, but I see your point (we could make the same point with
titlecaseFirstChar
). The thing is,
replaceFirstChar
is a general function which can be combined in a more flexible way. It makes sense to understand actual needs before releasing functions in the stdlib.
That said, I agree that pascal/camel/snake case conversions would be useful to many people, as well as capitalization of single-word strings