Skip to main content

Types

Overview

Swirl is statically and strongly-typed. Its first-class types are:

Integral Types

IDSize (in bytes)Description
i818-bit signed integer
u818-bit unsigned integer
i16216-bit signed integer
u16216-bit unsigned integer
i32432-bit signed integer
u32432-bit unsigned integer
i64864-bit signed integer
u64864-bit unsigned integer
i12816128-bit signed integer
u12816128-bit unsigned integer
bool1either true or false

Floating Point (FP) Types

IDSize (in bytes)Description
f32432-bit FP type.
f64864-bit FP type.

Array Type

Swirl provides the following syntax to create an in-place array for a type:

// `T` is the type and `N` is the number of elements.
let arr: [T | N] = [x, y, z, ...];

You do not need to declare the type explicitly if you provide an initializer, the compiler can infer the array-type:

let arr = [1, 2, 3, 4];  // automatically inferred to [i32 | 4]

Slice Type

Slices provide a "view" for a sequence of elements, internally, a slice is akin to a struct containing a pointer (to the first element) and a size. They are syntactically represented as: &[T] (for immutable slices) or &mut [T] (for mutable slices).

Reference Type

A reference-object redirects operations performed on it to the object it's bound to. A reference cannot be reassigned once it has been created. A reference type is syntactically represented as &T (for immutable references) or &mut T (for mutable references).

Struct Types

The following example demonstrates how struct types are defined:

struct MyStruct {
let a: i32;
let b: i64;

fn get_a(&self) { return self.a; }
fn get_b(&self) { return self.b; }

// static methods simply do not have `&self` (or `&mut self`) as the first parameter
fn construct(v1: i32, v2: i64) {
var tmp: MyStruct;
tmp.a = v1;
tmp.b = v2;
return tmp;
}
}


fn main() {
let instance = MyStruct::construct(8, 8);

var member = instance.a;
member = instance.get_a(); // a reference to `instance` is passed implicitly
}

Enum Types

Enums are scoped types used to group a series of named integral constants.

enum MyEnum {
ONE,
TWO,
THREE,
FOUR
}

fn main() {
var one: MyEnum = MyEnum::ONE;
}

The default underlying type is i32, one can change it (to another integral type) by doing:

enum MyEnum : i8 { 
...
}

C-Types

The following types exist to allow easier C-interop:

TypeC counterpart
c_intint
c_uintunsigned int
c_lllong long
c_ullunsigned long long
c_llong
c_ulunsigned long
c_size_tsize_t
c_ssize_tssize_t
c_scharsigned char
c_ucharunsigned char
c_shortshort
c_ushortunsigned short
c_bool_Bool
c_floatfloat
c_doubledouble
c_ldoublelong double
c_intptr_tintptr_t
c_uintptr_tuintptr_t
c_ptrdiff_tptrdiff_t
c_intmax_tintmax_t
c_uintmax_tuintmax_t
c_wchar_twchar_t

Stay informed