T
Tim Tyler
Roedy Green said:That inhibits any sort of optimisation on the read side since the
underlying substring address could change at any instant.
The address of any substring of a shared substring of a
string would remain fixed. Unless of course the substring
itself is modified.
That also adds overhead to every potential change to check for
dependent substrings.
IIRC, StringBuffer uses this strategy anyway, so this can't
be described as added overhead - this overhead is there in
the existing implementation today.
I could see you inventing a subStringBuilder, but I think
you would still want the various benefits of an immutable String.
The problems are all to do with concurrency. Those problems
could be dealt with in other ways besides making your
language's main string class immutable.
Immutability is such a fundamental notion that you probably
ought to be able to request an immutable object of *any* type.
This could be done by using an "immutable" modifier when
creating the object. In a more dynamic language you could
simply pass the object to a cryogenic cloning factory, to
obtain a frozen copy of it.
Immutable objects would never change - except when they are
deleted completely;
All non-primitive immutable objects would consist entirely
of other immutable objects.
Any attempts to modify them would produce exceptions.