Assignments
Simple assignment
Assignment
is used to change the value of a variable (local variable, parameters, class
attributes). Syntax is as follows:
variable =
evaluated_ expression;
The type of the term on the right must be the same as the type of the variable, in other words:
· it is identical to the type of the variable, or
· it is the variable child class, or
· there is an implicit conversion of the type of term on the right to the variable type.
The effect of an assignment depends on the variable class type:
· for primitive or basic classes (int, String, …), the assignment copies the value of the origin into the destination.
· for non primitive classes (classes of the metamodel, stream, …), the assignment only references the same object.
int i= 23;
int j;
Object[] E1;
Object[] E2;
Class CurrentClass;
Class MainClass;
j = i;
// Assignment of the content of i to j. i and j reference two
// different objects
MainClass = CurrentClass;
// These two variables reference the same object, which is
// one of the classes of the processed model.
E2 = E1;
// E2 contains all the elements of E1. They are copied
// if they are primitive objects, otherwise they are referenced.
Assignment attempt
Simple
assignment does not allow the assignment of a variable with a reference to a
type that is parent to the variable's type without risking errors, even if the
referenced object has a compatible type. To carry out
this type of assignment safely, J provides an operator that attempts
assignments. Syntax is as follows:
variable ?= evaluated_ expression;
With this operator, the variable takes the value of the term on the right if the type of this term is compatible with those of the variable. If there is type incompatibility, the variable is empty.
Example of an assignment attempt
In this example, we are in the context of a ModelElement and if the current object is a class, we want to display its features by calling the "Class:printFeature" method of the previous example.
ModelElement:printFeature()
{
Class c ?= this; // 'c' is assigned if
'this' is
// a 'Class'
c.<printFeature(); // 'printFeature' is only called
// if 'c' is a class.
}

Figure 3.
Extract of the metamodel used