Live data from Hacker News

The miracle of Smalltalk’s become: (2009)

gbracha.blogspot.com

11–20 of 30 posts

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

#11
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__

That works? Wow, I need to try this.

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

#12
> Take a minute to internalize this; you might misunderstand it as something trivial. This is not about swapping two variables - it is literally about one object becoming another. I am not aware of any other language that has this feature. It is a feature of enormous power - and danger.

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

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

#13
post #11

Earlier quoted context omitted.

Use ChainMap in __dict__

That works? Wow, I need to try this.

I got carried away: you can use ChainMap, but the __getitem__ is not going to get called: https://bugs.python.org/issue1475692

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.

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

#14

> Take a minute to internalize this; you might misunderstand it as something trivial. This is not about swapping two variables - it is literally about one object becoming another. I am not aware of any other language that has this feature. It is a feature of enormous power - and danger. I do think implementing or understanding this is trivial. 'Become' can be implemented by 1. swapping all references to 'a' and 'b',…

I usually use it as an example when people say Python is too dynamic to have a JIT, when Smalltalk was one of the percusors of JIT for dynamic languages with features like become: (SELF then took it even further).

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

#15
The magic here comes from Smalltalk's object memory system, which makes this object easy. (Well.. okay.. not completely easy, but simple.)

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.

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

#16
post #14

> Take a minute to internalize this; you might misunderstand it as something trivial. This is not about swapping two variables - it is literally about one object becoming another. I am not aware of any other language that has this feature. It is a feature of enormous power - and danger. I do think implementing or understanding this is trivial. 'Become' can be implemented by 1. swapping all references to 'a' and 'b',…

I usually use it as an example when people say Python is too dynamic to have a JIT, when Smalltalk was one of the percusors of JIT for dynamic languages with features like become: (SELF then took it even further).

If I'm not mistaken, this is also an amusing retort to the idea that only statically typed languages can have common refactoring tools.

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

#17
post #16
post #14

Earlier quoted context omitted.

I usually use it as an example when people say Python is too dynamic to have a JIT, when Smalltalk was one of the percusors of JIT for dynamic languages with features like become: (SELF then took it even further).

If I'm not mistaken, this is also an amusing retort to the idea that only statically typed languages can have common refactoring tools.

Specially since the first IDEs with such features were for Smalltalk and Lisp.

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

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

#18
In fact in most cases dynamic subclassing is enough for such things. In JavaScript that's achieved by changing __proto__ reference on an object.

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.

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

#19
post #14

> Take a minute to internalize this; you might misunderstand it as something trivial. This is not about swapping two variables - it is literally about one object becoming another. I am not aware of any other language that has this feature. It is a feature of enormous power - and danger. I do think implementing or understanding this is trivial. 'Become' can be implemented by 1. swapping all references to 'a' and 'b',…

I usually use it as an example when people say Python is too dynamic to have a JIT, when Smalltalk was one of the percusors of JIT for dynamic languages with features like become: (SELF then took it even further).

It's really unnerving when people repeat that "too dynamic" remark when there is widely known counterexamples. Fortunately it seems now people stopped denying the problem with the faster cpython project. IIRC, I think the roadmap for 3.12 or 3.13 already has some form of lightweight JIT.

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

#20

The magic here comes from Smalltalk's object memory system, which makes this object easy. (Well.. okay.. not completely easy, but simple.) 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 "smallt…

Squeak implements it with horrible performance, IIRC
Post reply on HN