Syntax - Practical/practical-sa GitHub Wiki
Integer types
U
(uppercase) followed by (bit) width for unsigned, S
for signed. Sizes are 8, 16, 32, 64 and 128. Examples:
S32
is C's int
on most modern platforms. U128
is C's unsigned long long
.
Implicit conversion between types is only possible if the destination type can hold all values the source type can. As such, U8
is implicitly convertible to all other unsigned types, as well as S16
and higher, but not to S8
, as the later cannot hold the numbers 128 through 255. Signed types cannot be implicitly converted to unsigned types regardless of width. The actual rules are somewhat more complicated, and are covered in here.
Integer literals without an expected types are automatically the smallest size that may hold them. In case both a signed and an unsigned type fit this bill, the unsigned type is preferred.
Statements and Expressions
Expressions
An expression is any sequence of operations or values that result in a value. This is, pretty much, the same was expressions are defined in any other language.
Casts
All casts are of the form keyword!type(expression)
. The keyword identifies the type of cast used:
Keyword | Cast name | Description |
---|---|---|
expect |
Safe cast | Hard set expected result type for contained expression. |
cast |
Static cast | Change the static type to type while preserving the semantic value as best the type allows. |
Statements
Statements are expressions without a value. One way to turn an expression into a statement is to add a semicolon (;
) at its end. Unlike other languages, expressions that evaluate to void
may be treated as expressions even without a semicolon.
Code blocks
A code block is a series of statements followed by one optional expression, grouped between an opening curly brace ({
) and a closing one (}
). If the code block ends with an expression, the code block as a whole is considered to have the value of that expression, and the code block itself is an expression, and can be used anywhere an expression can.
Example:
3 + { someFunction(); 7 }
Is a legal expression that equals 10
.
Functions
Functions are defined with the def
keyword, followed by the function name, its arguments in parenthesis, an arrow operator (->
) and the function's body inside curly braces:
def function(arg1 : S32, arg2 : U64) -> U8 {
0
}