Builtin Types

Builtin Types

Sun provides several builtin types that are available without explicit definition.

Primitive Types

Sun supports the following primitive types:

TypeDescriptionSize
i8, i16, i32, i64Signed integers1, 2, 4, 8 bytes
u8, u16, u32, u64Unsigned integers1, 2, 4, 8 bytes
f32, f64Floating-point numbers4, 8 bytes
boolBoolean (true or false)1 byte
voidNo value (for functions with no return)
var a: i32 = 42;
var b: f64 = 3.14159;
var c: bool = true;
var d: u8 = 255;

Arrays

Arrays are fixed-size collections of elements of the same type.

Declaration and Initialization

// Array literals
var arr = [1, 2, 3, 4, 5];
 
// With explicit type annotation
var arr: array<i32> = [10, 20, 30];
 
// Multidimensional arrays
var matrix: array<f64, 2, 3> = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];

Indexing

var arr = [10, 20, 30, 40, 50];
var first = arr[0];   // 10
var third = arr[2];   // 30
 
// Multidimensional indexing
var matrix = [[1, 2], [3, 4]];
var element = matrix[1, 0];  // 3
⚠️

Array bounds are checked at runtime. Accessing an out-of-bounds index will cause a runtime error.

Pointer Types

Sun provides pointer types for working with memory:

ptr<T> - Owning Pointer

An owning pointer with RAII semantics. Memory is automatically freed when the pointer goes out of scope.

var allocator = make_heap_allocator();
var p: ptr<Point> = allocator.create<Point>(3, 4);
// p.x, p.y accessible directly
// Memory freed automatically at scope exit

raw_ptr<T> - Raw Pointer

A non-owning raw pointer for low-level memory operations.

var raw: raw_ptr<i32> = allocator.alloc<i32>(10);
// Manual memory management required
_free(raw);

static_ptr<T> - Static Pointer

A pointer to immortal/static data, such as string literals. Represented as a fat pointer { ptr data, i64 length }. The data lives for the entire program.

Two methods take the fat pointer apart; they are the supported way to read a static_ptr:

MethodReturnsDescription
length()i64Number of elements (bytes, for a string literal; the NUL terminator is not counted)
raw()raw_ptr<T>The data pointer, for _load<T>, _print_bytes or a C function
var s: static_ptr<u8> = "hello world";
var n: i64 = s.length();        // 11
var bytes: raw_ptr<u8> = s.raw();
_print_bytes(bytes, n);

Passing a static_ptr<T> where a raw_ptr<T> is expected narrows it to raw() automatically, so a string literal can be handed straight to a C function (see C FFI).

Thread Type

Thread<T>

A handle to a spawned OS thread that will return a value of type T. Created via the spawn keyword.

var t: Thread<i32> = spawn(lambda() i32 { return 42; });
var result: i32 = t.join();  // Blocks until thread completes

See Threads for detailed documentation.

Classes in Sun are value types. Use ref to pass references to functions without copying.

Builtin Interfaces

Sun provides builtin interfaces for common patterns. These interfaces are always available and cannot be redefined.

IError

The error interface used with the try/catch/throw error handling system.

class DivByZero implements IError {
    function init() {}
    function code() i32 { return 1; }
    function message() String { return String("division by zero"); }
}
 
function divide(a: i32, b: i32) i32, IError {
    if (b == 0) {
        throw DivByZero();  // any class implementing IError
    }
    return a / b;
}
 
function main() i32 {
    try {
        var result = divide(10, 0);
        return result;
    } catch (e: IError) {
        return -1;  // Handle error
    }
}

Iteration interfaces

IIterator<T, Container> and IIterable<T, Self> are ordinary stdlib interfaces (stdlib/iterator.sun), not builtins, because their contract names Option<T>. See Iteration in the standard library reference and the for-in loop.

Reserved Type Names

The following type names are reserved and cannot be redefined:

  • IError - Error handling interface

Attempting to define a class or interface with this name will result in a compilation error:

// ❌ ERROR: Cannot redefine builtin interface 'IError'
interface IError {
    function code() i32;
}