Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
literals
Last updated at 1:40 am UTC on 17 January 2006
Literals are recognized by Smalltalk as instances of the objects they represent. It allows you to enter data into your program by means of program statements rather than I/O. When Smalltalk recognizes a literal it replaces it with an instance.
  1. number literal
  2. string literal
  3. character literal
  4. Symbol
  5. selector literal
  6. array literal
  7. Reserved identifiers

Literals and identity
Two strings containing the same sequence of characters are not neccesarily identical according to ==
That means that evaluating these three expressions in order in a workspace will probably answer false:
a:='abc'.
b:='abc'.
a==b.

But selecting all three and running them at once will return true, because squeak is optimizing the string litterals when compiling the code together. A compiled method or block returning a string litteral will always return the same instance, so evaluating these lines one at a time will return true:
block:=['abc'].
a:=block value.
b:=block value.
a==b.

This is by many means fine, but it has complications when you alter the return value from a block or method:
block:=['abc'].
a:=block value.
a at: 1 put: $A.
block value.

The last line will now return 'Abc' because it still returns the same (now altered) litteral, and this can lead to subtle bugs. A way to avoid the problem is to return a copy of the litteral:
block:=['abc' copy].
a:=block value.
a at: 1 put: $A.
block value.

This will ensure you a always returning what you want to return. This problem only happens with litterals of mutable types (arrays and strings), atomic and immutable types (numbers, characters and symbols) are always safe to return.