The miracle of Smalltalk’s become: (2009)
gbracha.blogspot.com
gbracha.blogspot.com
I do think implementing or understanding this is trivial. 'Become' can be implemented by 1. swapping all references to 'a' and 'b', or 2. swapping the data at memory locations of 'a' and 'b'. I can certainly see its interesting uses but don't see any value in describing it as a miracle or something that's hard to comprehend. Using the word 'become' to explain the language feature 'become' also isn't great.
I used ObjectStore at one company that does 'pointer swizzling' to lazy load deserialized objects, where segfault takes the place of `doesNotUnderstand`.
Mesa (XDE) and then Mesa/Cedar, have several references Xerox papers, on how those enviroments served as inspiration for their IDE like features, like REPL, typo corrections, debugging, code reloading,...
So "obj instanceof A" becomes "obj instanceof B".
As of persistence case I've solved it on JavaScript internal implementation level. In Sciter there is built-in JSON-ish data persistence module - close to Mongo-DB on feature set (modulo sharding).
Storage loads objects as half-backed proxies that contain only db references. Only when code tries to access props/methods of the loaded object it gets fetched from disk, its __proto__ is set to particular class, etc.
More on this architecture: https://gitlab.com/sciter-engine/sciter-js-sdk/-/blob/main/d...
By the way, dynamic subclassing is not a prerogative of only dynamic languages, here is how I did that in C++: https://stackoverflow.com/questions/21212379/changing-vtbl-o...
Patched QuickJS with storage support is here: https://gitlab.com/c-smile/quickjspp - it uses DyBase of Konstantin Knizhnik as a storage.
Become can swap two objects that are exactly of the same type, where the prototype change would be a null operation, achieving nothing.
Could you provide real life scenario when you will need that?
I mean to change all references to the object in the heap at runtime ...
So your only valid solution is to use __getattr__, which is guaranteed to work. But either you load all missing value on first access, and you can only lazy load once, or you pay the price of a method call for attribute access every single time. And in all cases, you won't have type hint doing the ork for you.
An hybrid strategy would be to always use __dict__ pointing to an empty dict you lazy load later, and @property for attribute that are only for the local object. Then you will pay the price of the of the method calls only for non lazy attributes.
Depending of your work load and data shape, one solution will be much better than the other, but nothing as elegant as the ChainMap.
Also... I know in Digitalk, you couldn't do a become: on SmallInteger because of the way their object memory worked, but don't remember if this was a limitation of SmallTalk-80 or Squeak.
Which is to say... when you have time, if you haven't done it already, do a search on "smalltalk object memory" or "Loom" or "bluebook object memory." There are some great references from the dawn of time.
Related to references and the above, Eliot Miranda's blog may be informative as it provides both details and sometimes code for many of the changes that have happened in the Squeak (and now OpenSmalltalk) VM, e.g. http://www.mirandabanda.org/cogblog/2013/09/13/lazy-become-a...
But lest someone read this thread and use it as evidence that Squeak is completely crappy, I would encourage them to do a bit of testing with a recent version. Also... I don't think people are going to use Smalltalk for it's raw performance, but in it's ability to model problems with pleasant "pure" OOP-ness.
Object.prototype.become = function(target) {
const newProto = Object.getPrototypeOf(target);
Object.setPrototypeOf(this, newProto);
for ( let key of Object.keys(this) ) {
delete this[key];
}
Object.assign(this, target);
}
Here's as close as I could get in JS. This fails Object.is(a,b) though, it just makes object A's data the same as B's.The two objects will desynchronize, unless all their properties are Objects (eg. Arrays) and no new ones are added.
As far as I know there's no way to update all references to an object to point to a new object. Though you could take the article's advice and add a bunch of indirection with getters and setters, referencing an internal "true" object which could be swapped out trivially.
In JS you're better served by proxies or to set things up ahead of time so you're not passing a direct object reference, but a mirror instead.
<https://bracha.org/mirrors.pdf>
Once you've got all relevant consumers using a mirror or a proxy and all access is therefore mediated, that's when you can freely perform these kind of swaps.
As noted by the commenters, however, this is quite a heavyweight solution.
Related:
> Inside the engine, a clever trick from Smalltalk called `becomes` is used to swap a newborn Proxy and an existing object that has arbitrarily many live references. Thus an object requiring no behavioral intercession can avoid the overhead of traps until it escapes from a same-origin or same-thread context, and only if it does escape through a barrier will it become a trapping Proxy whose handler accesses the original object after performing access control checks or mutual exclusion.
> The local jargon for such object/Proxy swapping is “brain transplants”.
Well, yes, that should be "the larger the reachable object graph you have, the more expensive become: becomes". It's not necessary to do the substitution in areas of the heap that are garbage.
I would rather do it without any sort of object table, because then you can do things like make some string object become the fixnum 42 everywhere. :)
If you're doing something with object persistence, you can't be doing individual become operations object-by-object. There has to be a batch API for doing a mass become where you pass a dictionary of what is to become what, and the objects are traversed once to do all the rewrites.