If I have a shared data structure like you would find in nested C structs, how would I best implement that in Mathematica?

I have some "object-oriented" code I would like to port to Mathematica. It's not really objected-oriented, except in the sense that there are parent objects and child objects inherit various fields from a common parent. So, for example, there is MovingObject which has velocity, heading, x_position, y_position, etc., and then there are various classes that inherit these common fields. So, you might have a car object, boat object, plane object, etc, all of which extend MovingObject.

The shared methods are non-existent or trivial, so the main thing is just how to share the movement-related data fields.

  • 1
    $\begingroup$ In this case, perhaps the most economical way would be to use Association-s with a field called "Type" (or just Type), possibly wrapped in inert symbol(s) if you need to use UpValues and overload some methods, or if you need mutability. Have a look at this example for such a use, for both mutable and immutable case. $\endgroup$ – Leonid Shifrin May 11 '16 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.