Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Everybody seems to be assuming a C++/Java-like object system, possibly because of the syntax used in the question, but it is quite possible to change the class of an instance at runtime in other languages.</p> <p>Lisp's CLOS allows changing the class of an instance at any time, and it's a well-defined and efficient transformation. (The terminology and structure is slightly different: methods don't "belong" to classes in CLOS.)</p> <p>I've never heard a name for this specific type of transformation, though. The function which does this is simply called <code>change-class</code>.</p> <p>Richard Gabriel <a href="http://www.dreamsongs.com/Files/clos-cacm.pdf" rel="nofollow noreferrer">seems to call it</a> the "change-class protocol", after Kiczales' <a href="http://www.cliki.net/AMOP" rel="nofollow noreferrer">AMOP</a>, which formalized as "protocols" many of the internals of CLOS for metaprogramming.</p> <p>People wonder why you'd want to do this; I see two big advantages over simply creating a new instance:</p> <ul> <li>faster: changing class can be as simple as updating a pointer, and updating any slots that differ; if the classes are very similar, this can be done with no new memory allocations</li> <li>simpler: if a dozen places already have a reference to the old object, creating a new instance won't change what they point to; if you need to update each one yourself, that could add a lot of complexity for what should be a simple operation (2 words, in Lisp)</li> </ul> <p>That's not to say it's always the right answer, but it's nice to have the ability to do this when you want it. "Change an instance's class" and "make a new instance that's similar to that one" are very different operations, and I like being able to say exactly what I mean.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload