"Singletons" package on SM
Avi Bryant
avi at beta4.com
Thu Dec 4 10:50:23 UTC 2003
On Dec 4, 2003, at 12:49 AM, Stéphane Rollandin wrote:
> I tried this in a workspace:
>
> | uh |
>
> Smalltalk recreateSpecialObjectsArray.
> uh _ Morph new.
> uh becomeSingleton.
> uh delegate: #extent to: World.
> uh extent.
>
> ... and it worked fine (returned the World extent)
I was unclear about this in my earlier email, but note that you don't
need to explicitly send #becomeSingleton. #delegate:to: will do that
for you.
> I also try those two:
>
> | uh |
>
> Smalltalk recreateSpecialObjectsArray.
> uh _ #().
> uh becomeSingleton.
> uh delegate: #size to: #(1 2 3).
> uh size.
Well, I've clearly got a bug when doing this for variably-sized
classes. What's happening is that although the anonymous subclass is
created, sending it #new: returns an instance of the original class (ie
Array, not our new subclass of Array). Then the delegation is
installed into the methodDictionary of Array, which in this case leads
to infinite recursion. Not good.
I'm not sure exactly what the bug is, yet, but I bet it has something
to do with class formats, and not using ClassBuilder. Anyone know why
this test fails?
TestSingletons>>testVariablySizedSingleton
|subclass|
subclass := Array singletonSubclass.
self assert: (subclass new: 5) class == subclass.
Behavior>>singletonSubclass
| meta subclass |
meta _ Metaclass new.
meta
superclass: self class
methodDictionary: MethodDictionary new
format: self class format.
subclass _ meta new.
subclass
superclass: self
methodDictionary: MethodDictionary new
format: self format.
^ subclass
More information about the Squeak-dev
mailing list
|