Two-way become in VAST

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Two-way become in VAST

Richard Sargent
Administrator
I'm pasting some code here so that if anyone has need of a two-way become, they can find it easily. This code is known to work under the 8.6.x versions. I expect it will continue to work under the 9.x, but I have not had occasion to verify it.

primTwoWayBecome: anObject
   
"Private.  Transform the receiver into the argument, and vice-versa.
     VA Smalltalk does not have a two-way become, but it can be simulated
     by #multiBecome."


   
(Array with: self with: anObject) multiBecome: (Array with: anObject with: self).

That part is quite straight-forward, but there are issues with identity hash under VA's become:.

twoWayBecome: anObject
   
"Private.  Transform the receiver into the argument,
     and vice-versa.  See documentation in Object>>primBecome:.
    This variant will swap the identities even if one or both of the objects' immutability bits are set.
    The immutability bits are restored afterwards, staying with the object's identity, not its state."


   
"Since we may be temporarily making this object mutable, must do this without interruption
    to prevent another thread from sneaking in another modification while mutable."


   
   
[| receiverWasImmutable argumentWasImmutable tempHash |
   
(receiverWasImmutable := self gbxIsImmutable)
        ifTrue
: [self gbxIsImmutable: false].
   
(argumentWasImmutable := anObject gbxIsImmutable)
        ifTrue
: [anObject gbxIsImmutable: false].

   
self primTwoWayBecome: anObject.
   
"The objects need to exchange their identity hashes.
     VA Smalltalk does not propagate them automatically."

    tempHash
:= self basicHash.
   
self basicHash: anObject basicHash.
    anObject basicHash
: tempHash.

   
self gbxIsImmutable: receiverWasImmutable.
    anObject gbxIsImmutable
: argumentWasImmutable]
            valueUnpreemptively
.
   
^self

Note that there are other methods invoked which are not included in this example.


Seth, you guys are entirely welcome to pick up this code to include it or a derivative of it in your product. (Although, you might want to plan out what you want to do with identity hash after a become:.)


--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/va-smalltalk/ef5671ed-7cc3-48f8-b06a-3fb57113fe19%40googlegroups.com.