The Freya Programming Language

Instantiation

See also

The new operator creates new instances for any concrete type. Freya supports two extensions to the traditional instantiation: object initializers and collection initializers.

new type-reference parametersopt collection-initializeropt

Object initializers

The instantiation syntax in Freya allows the inclusion of object initializers: assignments to properties or fields from the new instance that are included as if they were special parameters. This assignment includes an object initializer:

var D := new DataSet('MyDS', CaseSensitive := false);

The above statement is translated this way:

var D := new DataSet('MyDS');
D.CaseSensitive := false;

Object initializers must always be included after the regular parameter list.

Collection initializers

When the class to be instantiatied implements the generic ICollection[T] interface type, you can append a collection initializer to the instantiation expression:

var L := new List[Integer]![1, 2, 3];

Though List[T] has a constructor that accepts any IEnumerable[T] expression, including arrays, collection initializers are more efficient, since they avoid both the creation and initialization of a temporal array and the iteration over its items inside the constructor.

See also

The Freya Programming Language
Expressions
Constructors
Destructors
Literal arrays