4 ms·
> Why would you change the class of an object rather than create a new object as a replacement? Because there might be references to the object all over the pla
by kazinator 11y ago
> Why would you change the class of an object rather than create a new object as a replacement? Because there might be references to the object all over the place, and updating all of those references to point to the new object might be a lot of work or even impossible.
Logically speaking, updating all references to the new object would be equivalent to change-class.
If you update all references to an object to a new one, you cannot tell that there is a different object. In fact there is nothing to tell; it isn't a different object.
For instance if both X and Y in some expression (EQ X Y) previously point to the old object, then after this global update, both X and Y point to the new object. EQ returns T either way.
(Think about copying garbage collectors.)
- jsnell 11y agoI think you might have misinterpreted that paragraph somehow? It's not saying that updating all the reference to point to the old object would have the wrong effect. It's saying that updating the references might not be feasible. A garbage collector has more access than an application programmer... There is an interesting exception to that in Smalltalk, where the become: operation will replace all references to one object with references to another.
- kazinator 11y agoIt says clearly "change the class rather than X". So X is not the same changing the class, right? Traversing the heap and actually replacing references could actually be a good trade-off. It means that class objects don't need to be implemented as handles which point to the real implementation, just in case someone uses change-class. It depends on how often change-class is done. change-class could tie in to a generational garbage collector. If change-class is done on a baby object, we know that the tenured objects do not point backward to that object, so we don't have to traverse them. Only other baby objects (and of course areas like stack frames and registers).
- jsnell 11y agoRight. The two things are different despite having the same ultimate effect, since (in the context of Common Lisp) one is a generic operation provided by the language, while the other is application-specific with no language support and would need a fragile bespoke implementation for every single use. If the discussion was about Smalltalk that provides become: but not CHANGE-CLASS, it'd be a different question.
- ScottBurson 11y agoCLOS class instances are implemented with handles for a very important reason: so slots can be added to classes dynamically, even if live instances exist. In decades of Common Lisp programming I think I've written a CHANGE-CLASS call maybe twice, but I add slots to classes all the time (and occasionally remove them).
- dragonwriter 11y ago> A garbage collector has more access than an application programmer... That's generally true, although there's no reason it has to be: a runtime could provide an API giving the application programmer equivalent access. It would probably constitute a giant foot-gun, though.