[REBOL] Re: uppercase
From: brett:codeconscious at: 28-May-2002 14:45
Hi,
> >> x: "hello"
> == "hello"
> >> y: uppercase x
> == "HELLO"
> >> x
> == "HELLO" <====<<<< Why is x now uppercase?
>
> This is really unexpected behavior! Is this a bug?
No it is not a bug. It is actually a nice flexible design.
There are two things at work here.
(1)--------------
Look at these two lines of code:
a-symbol-for-the-string: "The String"
also-a-symbol-for-the-same-string: a-symbol-for-the-string
The word A-SYMBOL-FOR-THE-STRING is set to the string (reference to the
string).
The word ALSO-A-SYMBOL-FOR-THE-SAME-STRING is set to the value (after
evaluation) of the word A-SYMBOL-FOR-THE-STRING. Since this value is a
string (reference to the string), both word refer/set/point to the
exact same string.
BTW, The same behaviour occurs if instead of a STRING! you used a BLOCK!
- it happens for all series types.
(2)---------------
UPPERCASE, LOWERCASE, SORT, REVERSE modify the string that they are
given. It is *not* that they take a string, process it and return the
processed results. Instead, they take a string, modify that string, and
return a reference to that *same* string so that possibly another
function can use it as an argument.
For example:
>> x: "hello"
== "hello"
>> uppercase x
== "HELLO"
>> x
== "HELLO"
>> x: "hello"
== "hello"
>> sort uppercase x
== "EHLLO"
>> x
== "EHLLO"
The reference to the string that is returned in some cases is also
modified from the original. It is a reference to the same string but it
points to a different character in that string. In the case of REVERSE
for example, the index position points to the character after the
characters that were reversed:
>> x: "hello"
== "hello"
>> y: reverse/part x 3
== "lo"
>> x
== "lehlo"
>> same? x head y
== true
Now I said it was a flexible design. The reason is this. Imagine that
you have a huge string.
x: {An absolutely huge string...}
For UPPERCASE to do what you expected it would have to copy the string
it was given. So that just doing...
uppercase x
...would mean that you would have two huge strings in memory. Do you
have enough memory in your computer to complete the task? The designer
of these functions decided he could not answer this question so instead
leaves the choice to you. If you decide that you must not touch the
original string then you should make your own copy of it. Like this
>> x: "hello"
== "hello"
>> y: uppercase copy x
== "HELLO"
>> x
== "hello"
>> y
== "HELLO"
I hope that clears it up a bit.
Brett.