OUString is mutable?

Noel Grandin noel at peralex.com
Fri Sep 28 05:17:32 PDT 2012


On 2012-09-28 14:09, Michael Stahl wrote:
> On 28/09/12 13:46, Noel Grandin wrote:
>> Hi
>>
>> I don't really understand the point of not being able to mutate OUString
>> in-place, but being able to overwrite it via the assignment operator?
>>
>> It seems to me it should be one or the other.
>> Either OUString is immutable, and it cannot be assigned to, except via a
>> constructor, or it is mutable, and we can pretty much throw away
>> OUStringBuffer.
> [accidentally sent this first to Noel only instead of the list]
>
> the point you are missing is that OUString is a sort of "smart pointer"
> to an immutable buffer.  it works similarly to "String" in Java, e.g. in
> Java "String s = "foo"; s = "bar"" is legal but you cannot modify the
> content of whatever value is assigned to "s".
>
> that's why OUString has an operator= that makes it point at a different
> buffer but offers no way to write into the buffer (at least i hope it
> doesn't...).
>
>


That is exactly what makes it weird - it looks like a Java String, but 
it's not, because you can do this:

     void f(OUString s) {
          s = "2";
     }

     OUString s = "1";
     f(s);
     cout << s; // will print "2"

ie. the modification inside the method is visible outside the method.

Also, in Java, this
    String s = "1";
is really syntactic sugar for this:
    String s = new String("1");
So it's not a real assignment operation.

It just seems to me that we could easily enough steal a bit from the 
length field to indicate that the buffer is immutable, and then we could 
reduce our complexity by eliminating OUStringBuffer.


Disclaimer: http://www.peralex.com/disclaimer.html




More information about the LibreOffice mailing list