The site sections are under development.
The materials may be incomplete or contain errors. The site content will be synchronized with the implementation in the next release, while the current version (0.6.0) is intended for syntax prototyping and for creating the playground.
Creating objects and assignment
Creating objects
TrustLang uses several operators to create objects:
-
“:=” — creates or assigns a value to a variable/function (an object by value). If an object with such a name does not exist yet, it is created; otherwise the value is reassigned. Example:
x := 5;,hello(str) := { ... };. -
“::=” — declares a type (an alias or a typed type), for example
MyInt ::= :Int32;,Status ::= (OK=0, ERROR=1, BUSY=2,):Enum;. Using “::=” to create values/functions is impossible — the compiler reports: “cannot assign a type … to a value variable; use ‘::=’ to declare a type alias”.Also, a forward declaration of a native class is declared via
::=:String ::= %std::string { %size(): UInt64 := ...; };— the RHS is the class name (native — with a leading%), the body{ ... }— interface members (methods/fields/constructors), each:= ...(without a body). Constructing an object — calling the class name:s: String := String('hello');→std::string("hello"). More — in Native types.
The operators “::-” and “:-” are not implemented in the current version; earlier they were intended for pure functions and compile-time computed values.
Assigning a new value
The operator “=” is used to assign a new value to already existing objects. If an object with the specified name is absent, a compilation error occurs.
Value swap operator
The operator “:=:” (swap) does not create new variables, but only exchanges their values with each other.
The variables must have identical/compatible data types, or have the type :None, i.e. "_".
It is used to implement the copy-and-swap idiom, since no errors can occur when the operator is executed.
In the current implementation :=: is an exchange/move intrinsic (used as a statement; the result is
void): a :=: b expands to std::swap(a, b) (the types must be compatible, not necessarily
references; exchanging incompatible types is an error with a diagnostic); the form var :=: _ — moving the value
into discard: the move is performed REALLY (a temporary is materialized, which is immediately destroyed),
therefore var remains in a moved-from state, and for owning references (unique/shared) the resource is released.
See also “Reference types”.
Append element operator
The operator “[]=” — an analog of push_back: it appends a new element to the container on the left, increasing its size by one.
General rule: X []= v is equivalent to X.push_back(v).
- For a dictionary (
Dict, including positional “arrays”), an unnamed append adds a positional element:d []= 999. - For strings, it appends a character/string:
s []= 'd'→s.append("d"). - The right object of the operator must be compatible with the type of a single element of the left object
(string width: the narrow
'...'/ the wide"..."must match the type of the string container). - A nested LHS (
d['x'] []= v,d[0] []= v,d.field []= v) — not yet implemented (compilation error). - Dictionary merge via the expansion operator
...:d []= ... dict2ord []= ... (a=1, b=2,)adds all elements of the operand dictionary to the target (an analog ofextend/update); the size of the target grows by the number of elements (for a literal and a dictionary with a known size — it is checked statically).
d := (1, two=2,);
d []= 999; # add a positional element → size 3
d []= 'x'; # add one more element
d []= ... (a=100, b=200,); # merge a literal → size 5 (a and b added)
d2 := (three=3, 4,);
d []= ... d2; # merge a dictionary variable → size 7
s := 'abc';
s []= 'd'; # the string became "abcd"
@print('{}', s); # > abcd
Adding a named element of a dictionary is performed by the assignment operator d[name] = value (set/add by key).
For example, when defining the class :NewClass2:
:NewClass ::= :Class{ # Base class
field1:Int32 := 1; # class field
field2:Int32 := 2; # class field
method1():Void := {}; # method
};
:NewClass2 ::= :NewClass{ # inheritance
field2:Int32 := 2; # field field2 of the base class
method1():Void := {}; # method override (override)
};
$var := 99; # Create a temporary variable $var
{
$var := 100; # The new variable shadows the higher-level $var
@print('{}', $var) # > 100
}
@print('{}', $var) # > 99
Assigning a value to several variables at once and the dictionary unpacking operator
TrustLang supports the operation of assigning a value to several variables at once, which must be listed comma-separated to the left of the assignment operator.
On the right side of the assignment operator there can be only one value,
and to exchange the values of two variables, instead of writing a,b = b,a;
you must use the swap operator:
a :=: b;
As the right operand in the assignment operator, it is allowed to use the dictionary unpacking operator … (ellipsis), which can also be used when passing arguments to a function.