> 'capitalize(): String' is deprecated. Use rep...
# codingconventions
j
'capitalize(): String' is deprecated. Use replaceFirstChar instead
what is the thinking here?
Copy code
replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
is a lot clunkier
😕 1
m
I think the problem was with localization. I imagine something easier to use will come in the future when they create something like Locale.
j
that makes sense, thanks
m
The issue is with
capitialize()
being ambiguous - https://kotlinlang.slack.com/archives/C0B8Q383C/p1621263607077700
j
I hope that's not the actual reason, because that doesn't make sense at all
m
How it doesn’t make sense?
j
because it's not actually ambiguous
m
It can be either title casing or upper casing, that the ambiguity
j
i guess this was someone's pet peeve, but ruby, python, probably other languages use it to mean title case, i haven't noticed mass confusion in those communities
👆 1
if this was really something a docstring couldn't solve, then the obvious solution is to provide both .titleCase and .upperCase instead of neither
m
So here you go,
capitalize()
behaves differently in Kotlin from the languages that you mentioned
j
how so?
m
Capitalize
Ç„
and see the results. In Kotlin it isn’t transformed to
Ç…
j
so like mkrussel says this is more of a locale issue, than a "we don't know what capitalize means" issue
m
It’s a unicode issue
✅ 1
j
Also some languages capitalize every word, while Kotlin just titlecases the first char
j
fwiw, every language i've seen uses a different function name for that (e.g. python .title()), not .capitalize
f
It is ambiguous. Should title case only title case the first letter of the sentence? Every first letter of every word in the sentence? Only those of nouns? Should we uppercase or titlecase? What about conversions that cannot survive roundtrips (
.capitalize().decapitalize()
) due to language rules? And, and, and, … This are decisions only a user can make in their unique context and domain. Even things like case conversions have to be interpreted differently in different contexts (especially the roundtrip thing). Yes, it is a burden for users. But, having some implementation that works for everyone across the multiplatform world (where not even all targets have support for locales/language tags, and do not have Unicode databases) is simply impossible. Check out https://github.com/Kotlin/KEEP/issues/223 to catch up on the decision making. If JVM is all you care about then I would recommend Intl ICU. It contains all the advanced things to work with text. If ASCII is all you care about you can just uppercase the first and substring the rest. If …
837 Views