identity in Squeak

Martin McClure martin at hand2mouse.com
Sun Jun 16 23:21:46 UTC 2002


At 10:55 AM -0700 6/15/02, nicola farina wrote:
>hi all,
>just one question:
>
>why in Squeak evaluating the command
>  'fooBar' == 'fooBar'
>returns true (as if were Symbols)?

You are correct that Symbols are guaranteed to be identical if 
they're equal, and that Strings are not guaranteed to be equal. But 
they're not guaranteed to be not equal, either.

In Smalltalk, Strings are mutable, their characters can be changed 
with messages such as at:put:. However, literal strings are treated 
as though they are immutable, because changing them is a very bad 
idea. Some Smalltalks (VisualWorks 7, for instance) enforce the 
immutability of literal strings.

Squeak does not enforce immutability, but it does assume that literal 
strings will not be changed, so if it sees two equal literal Strings 
in the same compilation unit it only creates one String object to 
save space. The compilation unit in Squeak's current implementation 
is the method, so if you create two equal Strings in separate methods 
you will find that they are *not* identical.

For instance, try evaluating this in a workspace:

	foo _ 'foo'

Then evaluate:

	foo == 'foo'

It will return false.

  -Martin



More information about the Squeak-dev mailing list