[Instantiations, please be sure to preserve this functionality if you don't implement an official two-way become.]In porting GBS functionality from VisualWorks to VA Smalltalk, I needed two-way become support. Mark van Gulik posted an algorithm by which one can simulate two-way become using a one-way become (see below), but it required two copy operations and two separate become operations.
It turns out the #multiBecome: will do a two-way become if you convert a,b to b,a.
Example:
| date time dateHolder timeHolder |
date := Date today.
time := Time now.
dateHolder := Array with: date.
timeHolder := Array with: time.
(Array with: date with: time) multiBecome: (Array with: time with: date).
^Array with: dateHolder "now time" with: timeHolder "now date"
Mark's explanation of how to do it using brute force:
Just as a curiosity, simulating a two-way become with a one-way become
is a lot more efficient...
aPrime := A shallowCopy.
bPrime := B shallowCopy.
A oneWayBecome: bPrime.
B oneWayBecome: aPrime.
This one's cute because both objects are replaced with copies, and nobody
knows they're not the originals (like InvasionOfBodySnatchers). One of
the copies can be avoided, but it's not like that's going to be a major
bottleneck (unless you're fiddling with huge collections).