[E-Lang] Hydro & E: Update Syntax

Bill Frantz frantz@pwpconsult.com
Thu, 22 Mar 2001 12:24:31 -0800


At 5:28 PM -0800 3/21/01, Marc Stiegler wrote:
>By and large, it would work--and it would work exactly as
>
>    string += "abc"
>
>works in Java (ah....hmmm....does this actually work in Java? Never tried
>it).

Yes, and it is an exact illustration of the topic of discussion.  Note that
Strings in Java are immutable, ordered collections of characters.  What
happens under the covers with the above expression is:

StringBuffer _temp = new StringBuffer(string);
_temp.append("abc");
string = _temp.toString();

Note that the use of + for string concatination comes because Strings are
in bed with the Java compiler in ways that other types are not.  "The Java
Language Specification" (15.17.1) is quite interesting in how this magic is
defined.  The Java compiler always attempts to convert other types to type
String if they appear in an expression with a String.  If they are
primitive types (i.e. they are not Objects), then the compiler knows the
appropriate class to use to convert them to objects (int uses Integer etc.)
Once they are converted to a subclass of type Object, the toString() method
is used to convert them to strings.  If any of these steps result in a
value of null, the ASCII string "null" is used as the value.

The description goes on to say that:

1 + 2 + " fiddlers" ==> "3 fiddlers", while
"fiddlers " + 1 + 2 ==> "fiddlers 12".


>... whether a jump to only-immutables is a step too
>far for our target audience (I keep on saying, "No, this is not too big a
>step", but the question still nags at me).

Note that Java programmers are already used to immutable Strings.

Cheers - Bill


-------------------------------------------------------------------------
Bill Frantz       | Microsoft Outlook, the     | Periwinkle -- Consulting
(408)356-8506     | hacker's path to your      | 16345 Englewood Ave.
frantz@netcom.com | hard disk.                 | Los Gatos, CA 95032, USA