Live data from Hacker News

The miracle of Smalltalk’s become: (2009)

gbracha.blogspot.com

1–10 of 30 posts

Re: The miracle of Smalltalk’s become: (2009)

#2
"become" is a standard approach in Erlang, Akka and other actor model systems (because it is included in the original actor model axioms by Carl Hewitt, "[a message can] designate the behavior to be used for the next message it receives"). We write state machines with that (on some events, you just 'become' another state with its own reactions to messages).

Re: The miracle of Smalltalk’s become: (2009)

#3
This would've been very convenient for lazy loading in a recent python project. I ended up making the husk turn into a transparent wrapper for the loaded object, unfortunately adding a layer of indirection to every subsequent call. Does anyone here know a better way to do lazy loading objects in python?

Re: The miracle of Smalltalk’s become: (2009)

#4

    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.

Re: The miracle of Smalltalk’s become: (2009)

#5
post #3

This would've been very convenient for lazy loading in a recent python project. I ended up making the husk turn into a transparent wrapper for the loaded object, unfortunately adding a layer of indirection to every subsequent call. Does anyone here know a better way to do lazy loading objects in python?

If all the types are the same shape (at the C level) you can swap the class and all attributes of the husk.

Re: The miracle of Smalltalk’s become: (2009)

#6
post #3

This would've been very convenient for lazy loading in a recent python project. I ended up making the husk turn into a transparent wrapper for the loaded object, unfortunately adding a layer of indirection to every subsequent call. Does anyone here know a better way to do lazy loading objects in python?

Use ChainMap in __dict__

Re: The miracle of Smalltalk’s become: (2009)

#7
post #4

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…

Getters and setters will also desynchronize for similar reasons (new properties being added/removed).

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”.

https://brendaneich.com/2010/11/proxy-inception/>

https://bugzilla.mozilla.org/show_bug.cgi?id=580128>

Re: The miracle of Smalltalk’s become: (2009)

#9
post #2

"become" is a standard approach in Erlang, Akka and other actor model systems (because it is included in the original actor model axioms by Carl Hewitt, "[a message can] designate the behavior to be used for the next message it receives"). We write state machines with that (on some events, you just 'become' another state with its own reactions to messages).

It helps a lot that there's limited way to inspect an actor in a way which is not mediated by the actor itself, so for starters you wouldn't bother, and even if you did the "replacement" actor can just reply in a way you'd expected.
Post reply on HN