Types

Basics of Bramble Types

Basics of Bramble Types

Primitives

Keyword Description
bool boolean
i8 signed 8 bit integer
i16 signed 16 bit integer
i32 signed 32 bit integer
i64 signed 64 bit integer
u8 unsigned 8 bit integer
u16 unsigned 16 bit integer
u32 unsigned 32 bit integer
u64 unsigned 32 bit integer
f64 64 bit float
string C Style String
null A special type that corresponds to the null keyword. It represents a pointer that is not pointing to any address.

Arrays

Bramble supports staticly sized arrays of any type. The syntax for an array type is [<type>; <size>]. For example, [i32; 4] creates an array of 4 i32 values. Getting an element in array is done via the [] operators. For example, arr[0] will return the 0th element of the array arr.

Example:

    let arr: [i64; 2] := [0, 1];
    let x: i32 := arr[0];

Structures

A structure is a named aggregate type consisting of 0 or more named fields, where each field as a type. Fields can be primtive, arrays, or structures.

struct MyStruct {
    field: i32,
    foo: AnotherStruct,
    barr: [i8; 5],
}

To access a field in the structure, use the access operator: foo.bar. For an array of structures you would use arr[0].bar. For a structure in a structure: foo.bar.fizz.

Raw Pointers

Like most modern languages, Bramble will have a safe system for handling references, but to build that system and to support FFI a form of raw pointers is necessary. Raw pointers simply represent and address to a location in memory and offer no safe guards.

There are two types of raw pointers:

Keyword Description
*const T Raw pointer to an immutable location in memory
*mut T Raw pointer to a mutable location in memory

A raw pointer can be created for any type in the langauge, including structures and arrays.

Creating a Raw Pointer

@const x will create a raw pointer to any variable x. If x has type T then @const x will have type *const T. x can be immutable or mutable.

@mut x will create a raw pointer to a mutable location x. x must be mutable.

    let mut x: i32 := 5;

    let cp: *const i32 := @const x; // Create a raw pointer to an immutable location

    let p: *mut i32 := @mut x; // Create a raw pointer to a mutable location

The following would fail, because x is immutable:

    let x: i32 := 5;

    let p: *mut i32 := @mut x; // Error: cannot make a mutable pointer to immutable variable

Dereferencing a Raw Pointer

The ^ operator dereferences raw pointers.

Mutable raw pointers can be used on the left side of mutations:

    let mut x: i32 := 5;
    let p: *mut i32 := @mut x;
    mut ^p := ^p * 2;  // This will change the value stored in the location `x`

Unsafe Blocks

In the future, Bramble will require the use of raw pointers to be contained within unsafe blocks, as is standard for many languages.


Last modified February 10, 2022: Null Type (9f161d2)