Language Overview
Sun uses a C-style syntax with explicit type annotations and modern features. Functions are declared with the function keyword and the methods of a class or interface with method, types follow variable names after a colon, and blocks are delimited by curly braces. The language prioritizes readability and unambiguity — there are no lossy implicit conversions (a number only widens on its own), class arithmetic uses explicit operator hooks, and there are no macros.
Identifier Naming
Identifiers starting with _ (underscore) are reserved for compiler builtins and cannot be used for user-defined names.
function _helper() i32 { ... } // ❌ ERROR: reserved identifier
var _count: i32 = 0; // ❌ ERROR: reserved identifier
function helper() i32 { ... } // ✓ OK
var count: i32 = 0; // ✓ OKReserved Words
The following words are keywords and cannot be used as names for variables,
functions, parameters, fields, classes, interfaces, enums, or modules. Using
one where a name is expected is reported as 'partial' is a reserved word and cannot be used as an identifier.
| Group | Words |
|---|---|
| Declarations | var, const, function, test_function, class, packed_class, partial, interface, enum, implements, public, def, declare, extern |
| Modules | manifest, module, using |
| Control flow | if, else, match, for, while, break, continue, return |
| Errors | Result enums, try expression, and match |
| Literals | true, false, null, this |
| Operators | and, or, not |
| Types | i8, i16, i32, i64, u8, u16, u32, u64, f32, f64, bool, char, void, array, ref, ptr, raw_ptr, static_ptr |
| Other | unsafe |
A few words only have meaning in one position and remain usable as names:
in (in a for loop), as (in an extern declaration) and method (at the
start of a class or interface member), so a field may still be called
method.
Three of these carry syntax that is easy to miss:
[ref x, const ref y](…) => …— a capture list in brackets before the parameter list borrows the named variables instead of copying them. Compound values can only be captured this way. See Captures.unsafe { … }is an expression, so it can produce a value:var p = unsafe { _malloc(n); };. See Unsafe Blocks.packed_classdeclares a class with no padding between fields, for binary layouts and C structs. See Packed Classes.
var partial = 1; // ❌ ERROR: 'partial' is a reserved word
var in: i32 = 5; // ✓ OK: `in` is only a keyword inside `for (...)`Primitive Types
Sun provides the following primitive types:
| Type | Description |
|---|---|
i8, i16, i32, i64 | Signed integers |
u8, u16, u32, u64 | Unsigned integers |
f32, f64 | Floating-point numbers |
bool | Boolean (true or false) |
char | One Unicode scalar value |
void | No return value |
Variable Declaration
Variables are declared with var and require explicit type annotations:
var x: i32 = 42;
var name: static_ptr<u8> = "Sun";
var flag: bool = true;A value that never changes is declared with const instead. It can be read,
borrowed with const ref, and moved, but never assigned to or changed in
place. See Constants.
const MAX_RETRIES: i32 = 3;
const greeting = String(alloc, "hello");Comments
Sun supports single-line and block comments:
// This is a comment
var x: i32 = 42; // Inline comment
/* Block comment,
possibly spanning multiple lines */
var y: i32 = /* inline */ 7;Block comments do not nest.