new operator

Use the EGL new operator to dynamically create storage of a specified type. If you are creating a reference type variable, new creates a reference to the storage; otherwise it creates the storage itself.

You can specify a set-value block with the new operator. Note that a set-value block automatically turns off the null flag for a nullable variable. For more information, see Set-value blocks. For examples, see "When to use the new operator" in this topic.

Syntax

Syntax diagram for the new statement
partName
The name of an EGL part.

When to use the new operator

The only efficient use of the new operator is when you use it to create storage without assigning a name to it. In the following example, a function takes a dictionary as its parameter:
func( new Dictionary{ key1 = 3, key2 = "Hello", key3 = false } );
In the following example, the record contains a field that is an array of another kind of record. In this case, you must use the new operator:
record CustomerArray
	myCustomers CustomerRec[] = 
    [ new CustomerRec { idNum = 101 }, 
      new CustomerRec { idNum = 102 } ];
end

The alternative, declaring CustomerRec variables to put in the array, is impossible. A initializer for the record cannot access any variables other than the fields within that record.

You can use the new operator in a variable declaration in the following manner:
myInt INT = new INT;
This declaration offers no benefit and one major disadvantage compared to a standard declaration such as the following:
myInt INT;

When you use the new operator, storage is reserved twice. First the left hand side of the assignment creates storage for an INT and names it myInt. Then the new operator creates a second INT, and assigns its value to the first. The second INT is not used again.

You can use the following declaration to create a nullable variable (indicated by the question mark) whose initial value is not null:
myInt INT? = new INT;
The left hand side creates a nullable variable. The new operator creates an INT that is not nullable and assigns its value to the first INT. You can get the same effect with the following line, which creates a single variable:
myInt INT?{};

Feedback