String class api redesign

Red IO redio.development at gmail.com
Fri Jun 2 14:11:36 UTC 2023


Since String is a locked down Class part of the standard library it cannot
be modified nor subclassed(final).
Also string directly has a lot of useful methods on hand that the more
general CharSequence does not.
This and the fact that CharSequence was only added in later versions
(compared to string) causes many libraries to use the type string as
arguments.
This makes working with different implementations of text awkward and
inefficient (since you often have to convert to string)
There are multiple possible approaches to resolve this issue.

One solution could be to add many of strings useful methods to CharSequence
and implement them with default methods based on the existing abstract
methods (or throw exception). These defaults could than be overridden by
implementers that have more context to provide a more efficient
implementation.

Another approach could be to add a new interface that mirrors strings
methods and is implemented by string decoupling the method api of string
from the fixed string class.

An even more radical and most likely most difficult/breaking approach would
be to make string itself an interface. This solution would have the upside
that all existing libraries would suddenly accept the interface instead of
the locked down class without migrating each library seperatly. The
downsides would be that previous to this change all constructors would need
to be made private and some code that either used the constructors or was
dependant on the class file structure of string might break. Also string
literals would need to be rerouted to create an instance of the then
internal implementation of string instead.

There sure are other possibilities but those 3 where those I came up with.

They all achieve the same goal of allowing libraries to ask for something
string like without specifying the explicit implementation (similar to the
collections framework) this would allow seamless use of other
implementations in libraries without them having to reimplement or work
around existing methods from the string class.

Great regards
RedIODev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/discuss/attachments/20230602/3c615b7e/attachment.htm>


More information about the discuss mailing list