Expressions
Expressions work like in C, with one exception: it is possible to take the address of a temporary. This uses the operator &&
rather than &
.
Consequently, this is valid:
Well-defined evaluation order
Expressions have a well-defined evaluation order:
- Binary expressions are evaluated from left to right.
- Assignment occurs right to left, so
a = a++
would result ina
being unchanged. - Call arguments are evaluated in parameter order.
Compound literals
C3 has C’s compound literals, but unlike C’s cast style syntax (MyStruct) { 1, 2 }
,
it uses C++ syntax: MyStruct { 1, 2 }
.
Arrays follow the same syntax:
Note that when it’s possible, inferring the type is allowed, so we have for the above examples:
One may take the address of temporaries, using &&
(rather than &
for normal variables). This allows the following:
Passing a slice
Passing the pointer to an array
Constant expressions
In C3 all constant expressions are guaranteed to be calculated at compile time. The following are considered constant expressions:
- The
null
literal. - Boolean, floating point and integer literals.
- The result of arithmetics on constant expressions.
- Compile time variables (prefixed with
$
) - Global constant variables with initializers that are constant expressions.
- The result of macros that does not generate code and only uses constant expressions.
- The result of a cast if the value is cast to a boolean, floating point or integer type and the value that is converted is a constant expression.
- String literals.
- Initializer lists containing constant values.
Some things that are not constant expressions:
- Any pointer that isn’t the
null
literal, even if it’s derived from a constant expression. - The result of a cast except for casts of constant expressions to a numeric type.
- Compound literals - even when values are constant expressions.
Including binary data
The $embed(...)
function includes the contents of a file into the compilation as a
constant array of bytes:
The result of an embed work similar to a string literal and can implicitly convert to a char*
,
void*
, char[]
, char[*]
and String
.
Limiting length
It’s possible to limit the length of included with the optional second parameter.
Failure to load at compile time and defaults
Usually it’s a compile time error if the file can’t be included, but sometimes it’s useful to only optionally include it. If this is desired, declare the left hand side an Optional:
my_image
with be an optional IoError.FILE_NOT_FOUND?
if the image is missing.
This also allows us to pass a default value using ??
: