About at: and basicAt difference
Lex Spoon
lex at cc.gatech.edu
Sun Jan 13 15:40:34 UTC 2002
ducasse <ducasse at iam.unibe.ch> wrote:
> Hi
>
> I was thinking that Object>>at: and Object>>basicAt: would be exactly the
> same. This seems true from a primitive point of view but not from the code
> explaining the primitive.
>
> Can somebody explain me why? It is a "bug" that would never occur since at
> and basicAt are special primitives?
It's a convention that methods named #basicFoo are never overriden;
thus, you can access low-level functionality on any object by using the
#basicFoo version of a method. Usually you want to use #foo instead of
#basicFoo, so that the receiving object can decide for itself how to
respond -- in fact this is at the heart of objects! Nevertheless,
sometimes you want to ask for the low-level functionality explicitly,
instead of letting the object do something intelligent. (Like when my
undergrad college told us to install such and such a Windows DLL to
access their network, even though it was a generic TCP/IP network that
any computer can use. If they'd just ask, instead of trying to get
their hands on our computers....) As one quick example, sometimes an
implementation of #at: will find #basicAt: to be useful.
It is interesting, in Squeak, to look at implementors-of basicAt:
(there's only one!) and at: (lots!), and then to look at senders-of each
of these messages, to see how the methods are used.
-Lex
> Which one is in sync with the primitive 60
>
> Object>>at: index
> "Primitive. Assumes receiver is indexable. Answer the value of an
> indexable element in the receiver. Fail if the argument index is not an
> Integer or is out of bounds. Essential. See Object documentation
> whatIsAPrimitive."
>
> <primitive: 60>
> index isInteger ifTrue:
> [self class isVariable
> ifTrue: [self errorSubscriptBounds: index]
> ifFalse: [self error: (self class name) , 's are not
> indexable']].
> index isNumber
> ifTrue: [^self at: index asInteger]
> ifFalse: [self errorNonIntegerIndex]
>
>
> Object>>basicAt: index
> "Primitive. Assumes receiver is indexable. Answer the value of an
> indexable element in the receiver. Fail if the argument index is not an
> Integer or is out of bounds. Essential. Do not override in a subclass.
> See
> Object documentation whatIsAPrimitive."
>
> <primitive: 60>
> index isInteger ifTrue: [self errorSubscriptBounds: index].
> index isNumber
> ifTrue: [^self basicAt: index asInteger]
> ifFalse: [self errorNonIntegerIndex]
More information about the Squeak-dev
mailing list
|