How do custom data types work?

I’m trying to make a custom data type for polynomial expressions (i.e. 2x+1y^2+2) but i remembered i totally dont know what im trying to do. Can anyone give me a basic rundown on how ADTs work in Snap!?

You use the OOP library to create an object.
The CLONE block inherits from another object also with its own fields.
The _type field of an object is text used by IS … A … ? and ADT slots. It’s the actual type of your custom object

The _morph field is a procedure which returns anything, and that will be used for the list view rather than the default view.

I feel like my explanation was not good enough, so LMK if it doesn’t suffice

you can use other fields in the OOP object inside of this procedure, so you can make a template version of your type (with _type and _morph entries) and store it in a variable. Then, when you want to make a new instance of your type, you CLONE the template with the new information - e.g. fields called “x coefficient”, “y coefficient”, and “constant”. Your procedure under _morph will be called when Snap is trying to display your type in a reporter speech balloon or a speech balloon on the stage. Your morph entry, in this case, might do something like:

((join (x coefficient)[x + ](y coefficient)[y + ](constant)@<:>)@>)

Important: The text string or other visual representation of your custom type is not it’s actual value. While your type may look like a string, or a costume, or a whatever, to the program it remains an OOP object and should be treated as such.

One cool little feature of the _type field is that a custom type can have multiple types. Let’s say you CLONE your template type “shape” and specify a new _type field “triangle”. Your new object will be both a shape and a triangle.

If this isn’t clear, my best recommendation would be to import the shapes library to see how custom types are handled there.