Variables & Data Types
Chuks provides strictly typed data containers with type inference support.
Variables and Constants
Section titled “Variables and Constants”// Mutable variable with explicit typevar age: int = 25;
// Mutable variable with type inferencevar name = "Alice";
// Immutable constantconst PI = 3.14159;
// Constants must be initialized and cannot be reassignedconst MAX_SIZE: int = 100;Basic Data Types
Section titled “Basic Data Types”Chuks provides a comprehensive set of data types for precise memory and performance control.
| Category | Types | Description | Example |
|---|---|---|---|
| Integers | int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, byte, rune | Signed & unsigned whole numbers | 42, -7, 0 |
| Floats | float, float32, float64 | Floating-point numbers | 3.14, -0.5 |
| Complex | complex64, complex128 | Complex numbers | 1 + 2i |
| String | string | Text strings (UTF-8) | "hello", "world" |
| Boolean | bool | Boolean values | true, false |
| Dynamic | any | Dynamic type (use sparingly) | Any value |
| Void | void | No return value | Used in signatures |
| Null | null | Absence of value | null |
Numeric Literals
Section titled “Numeric Literals”In addition to standard decimal integers and floats, Chuks supports hexadecimal, octal, and binary integer literals.
| Prefix | Base | Digits | Example | Decimal Value |
|---|---|---|---|---|
0x | Hexadecimal | 0-9, a-f | 0xFF | 255 |
0o | Octal | 0-7 | 0o77 | 63 |
0b | Binary | 0-1 | 0b1010 | 10 |
// Hexadecimal (base 16)var hex = 0xFF; // 255var color = 0xABCD; // 43981var lower = 0xff; // 255 (lowercase also accepted)
// Octal (base 8)var perm = 0o755; // 493var oct = 0o77; // 63
// Binary (base 2)var flags = 0b1010; // 10var byte_val = 0b11111111; // 255var bit = 0b10000000; // 128All numeric literal forms work seamlessly in arithmetic and bitwise expressions:
println(0xFF + 1); // 256println(0o10 * 2); // 16println(0b1010 - 5); // 5println(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; // 51966var oct: int = 0o644; // 420var bin: int = 0b10101010; // 170Collections
Section titled “Collections”For detailed documentation on collection types, see:
- Strings — Immutable character sequences
- Arrays — Ordered, dynamic collections
- Maps — Key-value pairs
The dataType Construct
Section titled “The dataType Construct”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.
Basic Usage
Section titled “Basic Usage”dataType User { id: int; name: string; isActive: bool;}
// Instantiate using object literal syntaxvar u: User = { "id": 1, "name": "Alice", "isActive": true};
print(u.name); // "Alice"Generic Data Types
Section titled “Generic Data Types”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}Constructor-less Initialization
Section titled “Constructor-less Initialization”dataType does not have constructors. Instead, you initialize instances using map literals { key: value }. Types are checked against the dataType schema.
Methods (Optional)
Section titled “Methods (Optional)”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
Section titled “Nullable Types”Nullable types allow a variable to hold either a value of a specific type or null. Append ? to any type to make it nullable.
Basic Usage
Section titled “Basic Usage”var name: string? = "Alice";name = null; // Valid — string? accepts null
var age: int? = null;age = 25;Function Parameters and Return Types
Section titled “Function Parameters and Return Types”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;}In DataTypes
Section titled “In DataTypes”dataType User { id: int; name: string?; email: string?;}
var user: User = { "id": 1, "name": "Alice", "email": null};In Classes
Section titled “In Classes”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);In Interfaces
Section titled “In Interfaces”interface UserService { findById(id: int): User?; getName(id: int): string?;}Nullable Arrays
Section titled “Nullable Arrays”// Array of nullable stringsvar tags: []string? = ["hello", null, "world"];
// Nullable array itselfvar items: []int? = null;Type Conversions
Section titled “Type Conversions”Chuks provides Go-style builtin functions for converting between types:
| Function | Returns | Description | Example |
|---|---|---|---|
int(value) | int | Convert to integer (truncates) | int(3.14) → 3 |
float(value) | float | Convert to float | float(42) → 42.0 |
string(value) | string | Convert any value to string | string(42) → "42" |
Conversion Rules
Section titled “Conversion Rules”// Float to int (truncates)var x: int = int(3.99) // 3
// Int to floatvar y: float = float(42) // 42.0
// String to intvar z: int = int("123") // 123
// String to floatvar w: float = float("3.14") // 3.14
// Any value to stringvar s: string = string(42) // "42"var s2: string = string(true) // "true"
// Boolean conversionsvar b1: int = int(true) // 1var b2: int = int(false) // 0var b3: float = float(true) // 1.0These conversions work in both the VM interpreter and AOT-compiled code.