Skip to content

Variables & Data Types

Chuks provides strictly typed data containers with type inference support.

// Mutable variable with explicit type
var age: int = 25;
// Mutable variable with type inference
var name = "Alice";
// Immutable constant
const PI = 3.14159;
// Constants must be initialized and cannot be reassigned
const MAX_SIZE: int = 100;

Chuks provides a comprehensive set of data types for precise memory and performance control.

CategoryTypesDescriptionExample
Integersint, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, byte, runeSigned & unsigned whole numbers42, -7, 0
Floatsfloat, float32, float64Floating-point numbers3.14, -0.5
Complexcomplex64, complex128Complex numbers1 + 2i
StringstringText strings (UTF-8)"hello", "world"
BooleanboolBoolean valuestrue, false
DynamicanyDynamic type (use sparingly)Any value
VoidvoidNo return valueUsed in signatures
NullnullAbsence of valuenull

In addition to standard decimal integers and floats, Chuks supports hexadecimal, octal, and binary integer literals.

PrefixBaseDigitsExampleDecimal Value
0xHexadecimal0-9, a-f0xFF255
0oOctal0-70o7763
0bBinary0-10b101010
// Hexadecimal (base 16)
var hex = 0xFF; // 255
var color = 0xABCD; // 43981
var lower = 0xff; // 255 (lowercase also accepted)
// Octal (base 8)
var perm = 0o755; // 493
var oct = 0o77; // 63
// Binary (base 2)
var flags = 0b1010; // 10
var byte_val = 0b11111111; // 255
var bit = 0b10000000; // 128

All numeric literal forms work seamlessly in arithmetic and bitwise expressions:

println(0xFF + 1); // 256
println(0o10 * 2); // 16
println(0b1010 - 5); // 5
println(0xFF & 0x0F); // 15 (bitwise AND)
println(0b11110000 | 0b00001111); // 255 (bitwise OR)

You can store them in typed variables just like any other integer:

var hex: int = 0xCAFE; // 51966
var oct: int = 0o644; // 420
var bin: int = 0b10101010; // 170

For detailed documentation on collection types, see:

  • Strings — Immutable character sequences
  • Arrays — Ordered, dynamic collections
  • Maps — Key-value pairs

dataType is a lightweight structure for grouping data fields. It’s similar to structs in Go or records in TypeScript. Unlike classes, dataType is intended for simple data containers.

dataType User {
id: int;
name: string;
isActive: bool;
}
// Instantiate using object literal syntax
var u: User = {
"id": 1,
"name": "Alice",
"isActive": true
};
print(u.name); // "Alice"

Data types support generics for flexible data structures.

dataType Point<T> {
x: T;
y: T;
}
dataType Pair<K, V> {
first: K;
second: V;
}
function main() {
// Point of integers
var p1: Point<int> = { "x": 10, "y": 20 };
// Point of strings
var p2: Point<string> = { "x": "ten", "y": "twenty" };
// Pair of different types
var kv: Pair<string, int> = { "first": "age", "second": 30 };
print(p1.x); // 10
print(kv.second); // 30
}

dataType does not have constructors. Instead, you initialize instances using map literals { key: value }. Types are checked against the dataType schema.

While primarily for data, dataType can contain methods if needed.

dataType Vector {
x: int;
y: int;
function magnitude(): float {
return math.sqrt(this.x * this.x + this.y * this.y);
}
}

Nullable types allow a variable to hold either a value of a specific type or null. Append ? to any type to make it nullable.

var name: string? = "Alice";
name = null; // Valid — string? accepts null
var age: int? = null;
age = 25;
function findUser(id: int): string? {
if (id == 1) {
return "Alice";
}
return null;
}
function greet(name: string?): string {
if (name == null) {
return "Hello, stranger!";
}
return "Hello, " + name;
}
dataType User {
id: int;
name: string?;
email: string?;
}
var user: User = {
"id": 1,
"name": "Alice",
"email": null
};
class UserProfile {
var name: string;
var bio: string?;
constructor(name: string, bio: string?) {
this.name = name;
this.bio = bio;
}
}
var profile = new UserProfile("Alice", null);
interface UserService {
findById(id: int): User?;
getName(id: int): string?;
}
// Array of nullable strings
var tags: []string? = ["hello", null, "world"];
// Nullable array itself
var items: []int? = null;

Chuks provides Go-style builtin functions for converting between types:

FunctionReturnsDescriptionExample
int(value)intConvert to integer (truncates)int(3.14)3
float(value)floatConvert to floatfloat(42)42.0
string(value)stringConvert any value to stringstring(42)"42"
// Float to int (truncates)
var x: int = int(3.99) // 3
// Int to float
var y: float = float(42) // 42.0
// String to int
var z: int = int("123") // 123
// String to float
var w: float = float("3.14") // 3.14
// Any value to string
var s: string = string(42) // "42"
var s2: string = string(true) // "true"
// Boolean conversions
var b1: int = int(true) // 1
var b2: int = int(false) // 0
var b3: float = float(true) // 1.0

These conversions work in both the VM interpreter and AOT-compiled code.