String literal at:put: question

Ivan Tomek ivan.tomek at acadiau.ca
Mon Dec 25 17:19:01 UTC 2000


>     'abcdef'
>         at: 1 put: $x;
>         yourself
> which returns the receiver of the first message expression.

Correct. More generally, messages that perform add/delete/modify and similar 
operations on collections work in the same way - they change the collection but 
return the argument. So, for example,

| oc newOC x y |
oc := OrderedCollection new.
x := oc add: 15.
y := oc removeLast.
newOC := oc add: 30; yourself

causes x and y to become 15 while changing oc as you would expect, and 
newOC becomes an OrderedCollection containing the single element 30.

Many people find this unexpected and even experienced Smalltalk programmers 
occasionally forget this. 

To be consistent with others you should follow this convention, even with your 
own creations such as

aLibrary addBook: aBook	

A Smalltalk programmer would expect that this changes aLibrary and return 
aBook - although there is nothing to prevent you from writing it so that it returns 
the modified aLibrary.


Ivan
.

Ivan Tomek,
Jodrey School of Computer Science
Acadia University
Nova Scotia, Canada

fax: (902) 585-1067
voice: (902) 585-1467
e-mail: ivan.tomek at acadiau.ca





More information about the Squeak-dev mailing list