3 ms·
In object-oriented languages (like c++ or java), changing an object's class usually involve "up-casting" or "down-casting". Such operations are pretty much by d
by joe_the_user 11y ago
In object-oriented languages (like c++ or java), changing an object's class usually involve "up-casting" or "down-casting". Such operations are pretty much by definition violations of some earlier architectural plan - either the plan of the program or the plan of some library you are using (the code in the article is modifying a thing passed to it by a third party library it seems). That doesn't say they are "wrong" since it may be more cost-effective to fix things "on the fly" but it seems like they are generally an indication you are entering the realm of "technical debt", stretching a plan beyond it's initial intentions.
- kazinator 11y agoCasting in C++ doesn't change an object's type. It changes the type of the reference or pointer through which the object is viewed. The run-time-type-info (RTTI) of the object is the same. Lisp change-class really changes the type. (There are no typed references in Lisp that correspond to what's going on in C++ with up and down casting; the object is just the object.)
- jlg23 11y agoThe author provides a good example of legitimate use of change-class with his compiler example. About violating "some earlier architectural plan" in the case of libraries: Being able to do this allows CL programmers to freely mix libraries with different "architectural plans" without resorting to some half-baked fusion architecture that has to account for the different styles used by the libraries. Last but not least, change-class is not a hack - it's behavior is well defined and documented. And when you consider that CL's "object orientation" is more generic that c++'s or java's, based on generic methods alone, inferring from c++- or java-paradigms to good CL style just does not work. Peter Seibel's talk "Practical Common Lisp" at google explains that pretty well: https://www.youtube.com/watch?v=VeAdryYZ7ak https://www.youtube.com/watch?v=VeAdryYZ7ak
- EdwardCoffin 11y agoIt's the kind of thing that is more useful in an image-based language. It can be very useful for debugging a running instance you've invested a lot of effort in. I've never used it in production code though. You could compare it to using a debugger to change the value of a variable in C or C++ in order to proceed with a program's run. For instance, you might notice that a your program should have updated a variable to correspond to some other data that was updated, so you make a mental note to add some code that will do that, manually change the variable yourself, and proceed. Edit: this post is probably a response to the recent post on the same topic: https://news.ycombinator.com/item?id=9928485 https://news.ycombinator.com/item?id=9928485
- yoklov 11y agoI've seen code that wrote directly to the vtable pointer (in c++). The code knew what it was doing was dangerous, but jeez. Always made me uncomfortable. FWIW I usually just use an enum. Easy to change on the fly, easy on the cache, easy to serialize, doesn't require indirection, ... Lots of benefits. Pisses off switch-phobic programmers, though.
- kazinator 11y agoChange class is simulated in C++ via the State Pattern and possibly others. Any pimpl variant can achieve an effective change class by allowing an object's internal representation to be mutated to a new representation. I once made a list/binary search tree library called Austin, purely in C. This library lets you dynamically change the type of an object from one kind of binary search tree to another. For instance from Red-Black to AVL or Splay. Or to a sorted linked list. There is a "vtable" in there which gets swizzled to point to different operations. During the swizzling, there are special operations which handle the needed reorganizations of the tree.
- deleted 11y ago[deleted]
- dragonwriter 11y ago> In object-oriented languages (like c++ or java), changing an object's class usually involve "up-casting" or "down-casting". No, in static-typed OO languages that work like C++ and Java, "upcasting" and "downcasting" are means of telling the typechecker that you wish to use an object of one class as if it were a different class lower or higher in a class hierarchy, it doesn't have any effect on the actual class of the object.