Retrofitting objcaps

Andreas Raab andreas.raab at
Thu Oct 19 07:48:59 UTC 2006

Matej Kosik wrote:
> What about ConstantPoint? Constant point is such an object which behaves
> exactly like a Point except for that it cannot be moved (once it is
> created). Can we implement such a thing in Smalltalk? Yes, it is
> "enough" to implement E interpreter in Squeak and then you are done. Can
> we implement constant point directly in E? Yes, it is trivial (few lines
> of code).
> Or am I wrong? Is it somehow possible to implement ConstantPoint in
> Smalltalk?

Sure. It's not even hard and I can think of at least three methods 
depending on how far you want to go with the level of support. The 
easiest (and most illustrative, though not directly comparable) method 
is this:

makePoint: aPoint
   "Create a constant point object"
   | pointClass pointObj |
   pointClass := aPoint newUniclass.
   pointClass compile: 'x ^', aPoint x printString.
   pointClass compile: 'y ^', aPoint y printString.
   ^pointClass new

The other two methods are:
* Use literal sharing, e.g., the identical x, and y values. The 
difference to the above is that both x and y would be parsed again by 
the compiler and therefore may not be identical to the input
* Use blocks as accessors which would also use lexical scoping rules and 
be the most truthful (but least readable and hardest to implement 
directly) solution.

The main thing that's missing in Squeak to support this more easily is 
some "literal object" syntax by which you could create an anonymous 
class in a method and instantiate that class directly. This would allow 
you to reuse the same class over and over again, but it would not be 
much different from the above.

   - Andreas

More information about the Squeak-dev mailing list