Literals
Tusk uses the familiar notation for integer numbers,
for example…
var x := 123;
var y := -99;
var z := 12_345_678;
As shown above, you may include underscores to group the digits
of large numbers.
If the number is in the range -2,147,483,647…2,147,483,647
(i.e., -231 … 231-1),
then it will be of type Integer.
Otherwise, if the number is in the range
-9,223,372,036,854,775,808…9,223,372,036,854,775,807
(i.e., -263 … 263-1),
then it will be of type Int64.
Otherwise, if the number is in the range
±79,228,162,514,264,337,593,543,950,335,
then it will be of type Decimal.
Otherwise, it will be of type Double.
In summary, the following types are used, in order:
Integer, Int64, Decimal, and Double.
Like Delphi, Tusk uses the dollar sign symbol to
express unsigned integers in hexadecimal notation…
var a := $FF;
var b := $ABACAB;
var c := $7FFF_FFFF_FFFF_FFFF;
Hex literals will be of type Cardinal when possible
(i.e. not greater than 232-1),
and UInt64 otherwise
(the maximum allowed value is 264-1).
A numeric literal that includes a decimal point
and/or uses scientific notation will be of type Double…
var a := 1.0;
var b := 1_234_567.890_777;
var c := -2e-9;
To define a literal of type Single or Currency,
simply use a typecast…
// MyFunc has three overloads: Double, Single, and Currency
MyFunc(2.5); // Calls the Double overload
MyFunc(Single(2.5)); // Calls the Single overload
MyFunc(Currency(2.5)); // Calls the Currency overload
As mentioned above,
an integer literal too big for Int64
will implicitly be a Decimal (if possible).
To make an explicit Decimal literal,
use the m suffix on an integer or
floating-point number…
var a := 123m;
var b := 3e-9m;
var c := 123_456m;
In Tusk, string and character literals
typically use single-quote characters, like in Delphi…
var a := 'hello';
var b := 'Goodbye'#33;
var c := 'That''s alright';
var ch := 'x';
As shown above, escape apostrophe characters by doubling
them up.
Also, characters can be referenced by their Unicode code
points, using the same notation as in Delphi
(with a pound sign).
A string literal with just one character is
of type Char, not string.
For example, in the above code, a, b, and c are
of type string, while ch is of type Char.
Char values are implicitly convertible to string,
and AnsiChar values are implicitly convertible to AnsiString.
Tusk also supports Delphi's relatively recent multi-line
staring literals…
Writeln('''
Line 1
Line 2
Line 3
''');
Finally, Tusk supports strings delimited with double-quote
characters, like in JavaScript…
var a := "hello";
var b := "yes\n\rno\n\rmaybe";
var c := "Press \"ENTER\" to continue...";
This style of string is provided for compatibility with JSON
(every valid JSON document is a valid Tusk program),
but should not be used in code that might benefit from
Delphi compatibility.
The nil keyword is a literal value compatible with
several data types: interfaces, dynamic arrays, pointers,
procedures, and functions.
Tusk offers a notation for expressing date and date/time
literals, which it borrows from MiniCalc…
var a := #(1/1/2000);
var b := #(2/3/2024 5:16pm);
The valid date and date/time formats are defined by Delphi's
TryStrToDate
and
TryStrToDateTime
functions.
Note that this syntax is not compatible with Delphi;
the above could be written as follows instead…
var a := StrToDate('1/1/2000');
var b := StrToDateTime('2/3/2024 5:16pm');
In Tusk, an array or set literal is expressed
with square brackets…
var a: TArray<Char> := ['a', '7', '?']; // an array
var b: TSysCharSet := ['a', '7', '?']; // a set
Tusk also supports Delphi's dynamic array constructor notation…
var x := TArray<Double>.Create(1, 2, 4.5);
In Tusk, use curly braces to build an IPropBag literal…
var Bag1 := { x=1, y=2, begin=11, end=99 };
var Bag2 := { "Background Color": "Green", "Size": 12 };
As shown above, property names may be identifiers, keywords,
or string literals.
The name and value may be separated by a colon or an equals sign…
{
x = 1,
y: 2,
z = 3,
}
Name/value pairs may be separated using commas or semi-colons…
var Bag1 := {
x = 1;
y = 2;
z = 3;
};
var Bag2 := {
a = 4,
b = 5,
c = 6,
};
Procedures and functions may be defined using the following
concise syntax…
var Bag := {
procedure Start = Writeln('hello');
function FullName =
GetComputerName + '.' + GetUserName;
procedure Print(x, y: Integer);
begin
Writeln(x);
Writeln(y);
end;
};
The above is equivalent to the following more verbose syntax…
var Bag := {
Start: procedure = Writeln('hello');
FullName: function =
GetComputerName + '.' + GetUserName;
Print: procedure(x, y: Integer)
begin
Writeln(x);
Writeln(y);
end;
};
IPropBag literals are not compatible with Delphi
(where curly braces are comments).
For code that might benefit from Delphi compatibility,
use PropBagBuilder or NewPropBag instead…
// Newer, single-expression approach...
var Bag1 := BuildPropBag
.Prop('a', 1)
.Prop('b', 2)
.Prop('c', 3)
;
// Older, multi-statement approach...
var Bag2 := NewPropBag;
Bag2['a'] := 1;
Bag2['b'] := 2;
Bag2['c'] := 3;
⏱ Last Modified: 2/15 10:06:11 am