logo       

Re: [Object]why at:put:: msg#00231

lang.smalltalk.squeak.beginners

Subject: Re: [Object]why at:put:

math suen wrote:

>Hi,
>
>I was reading a bit of code and I saw the message at:put: in Object so I
>wonder why it's here.
>For me it should rader be in Collection.
>
>Thanks for setting me straight.
>
>
>
Mathew,

It has to do with how objects are structures in Smalltalk. Objects can
simply hold a fixed set of instance variable slots, or they can be
"indexed" (ie hold a fixed-sized set of slots indexed by an integer) or
a combination of the two (*). When you create a subclass of Object you
normally do something like:

Object subclass: #MyClass
instanceVariableNames: 'ivar1 ivar2'
classVariableNames: ''
poolDictionaries: ''
category: 'My Category'

but if you instead do something like Array does:

Object variableSubclass: #MyIndexedClass
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'My Category'

This creates a class whose instances are "indexed" (that is, you can
send them #at: and #at:put:). Now, if Object didn't have #at:put: we'd
have to implement it in every indexed subclass. So, in some sense,
#at:put: is there "just in case". If you send a non-indexed subclass
#at:put: you'll normally get an error (unless it has overridden it to do
something useful).

o := MyClass new.
o at: 1 put: 'blah'

causes the error "Instances of MyClass are not indexable". But:

o := MyIndexedClass new: 10.
o at: 1 put: 'blah'

works just fine. BTW, yes, you will get an error if you tried to send
new: to MyClass rather than just new. Play with this code a bit.
Inspect the objects being created to see how they differ. I hope that
this helps...

David

(*) actually there are other options such as word-sized indexed etc


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise