[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: how expressive are they?




On Mon, 25 Nov 2002, Anton van Straaten wrote:

> Now it's possible to use (swap a b) to swap the values of two variables.
>
> Succinct closures don't help here, and nor does "easy syntax for
> assignment", except in the sense that you could forget about the macro and
> just write the code inline.  In a language that doesn't support call by
> reference, a swap would be impractical or impossible without macros.  In
> Smalltalk, for example, it may not be directly possible (?)  Someone seems
> to have supported this by implementing "reified variables":
> http://wiki.cs.uiuc.edu/VisualWorks/Reified+Variables - but there's a *lot*
> of code to support it (420-odd lines, although it supports more than just
> swapping variable values).  Perhaps Smalltalk could use a macro system? ;)

Well, I wouldn't complain ;).  But the "reified variables" work is more
the point: the culture in Smalltalk is to make the language extensible by
making the underlying structures directly available and manipulable at
runtime, rather than through compile-time tricks like macros.  I like to
point out that I implemented call/cc in Smalltalk in about 10 lines of
code - macros don't help you there (and no, wrapping your entire program
and libraries in a macro that does CPS transformation does not count).
There doesn't happen to be a standard interface in Smalltalk for
interacting with bindings, so the code is a little clunky, but in Squeak
you could do something like this:

MethodContext>>swap: variableA with: variableB
  |tmp|
  tmp := self tempNamed: variableA.
  self tempNamed: variableA put: (self tempNamed: variableB).
  self tempNamed: variableB put: tmp.

MethodContext>>tempNamed: aString
  ^ self tempAt: (self tempNames indexOf: aString)

MethodContext>>tempNamed: aString put: anObject
  ^ self tempAt: (self tempNames indexOf: aString) put: anObject

And use it like this:

|a b|
a := 1.
b := 2.
thisContext swap: #a with: #b.