Why to put ".copy" to a type in a typesystem concept

Seen the subject in many places. So why should we copy a type node while using :==: in a typesystem concept?
0
2 comments
Avatar
Permanently deleted user
Do you have an example where you have seen that? .copy is often required when using quotations and because quotations can be useful in typesystem equations it's very likely that you have seen this.

A quotation is an easy way to instantiate a node and defining properties, childs and references. When you reference an existing node inside an antiquotation and don't create a copy, this node is set as a child of the newly created node and because every node can only have one parent, it is removed from its previous parent.

In this case the copy is not related to the typesystem equation.

More information about quotations http://confluence.jetbrains.net/display/MPSD25/Typesystem#Typesystem-Quotations
0
Avatar
Permanently deleted user
A type is a tree of nodes. It can be a part of an existing model, or it can be "detached" from it (using dot copy notation). Any two types are equal, when consist of the equal properties and children. The latter approach is safer, because at the moment MPS cannot seal nodes (make them immutable) and subsequent operations on types may modify the original model (if it happens in a command/write action).

So, the rule is: when you use a type you got from typesystem (using node.type operation), you should .copy it as soon as possible, especially if you are going to $COPY_SRC$ it in the generator, or to insert it into another model (inserting node somewhere detaches it from the original location).

If someone defines typeof(VarRef) :==: node.varDecl.type; and another guy writes an extract variable intention, most probably the original varDecl.type might disappear after the intention... That's why you see node.varDecl.type.copy.
0

Please sign in to leave a comment.