Types
Quant features a rich, statically-typed system that provides both safety and flexibility. All variables must have explicit type annotations.
Primitive Types
Section titled “Primitive Types”Integer Types
Section titled “Integer Types”Quant provides flexible integer types with customizable bit sizes and signedness.
Basic Integer
Section titled “Basic Integer”x: int; // 32-bit signed integer by defaultCustom Bit Size
Section titled “Custom Bit Size”small: int<8>; // 8-bit signed integermedium: int<16>; // 16-bit signed integerlarge: int<64>; // 64-bit signed integerhuge: int<128>; // 128-bit signed integerCustom Signedness
Section titled “Custom Signedness”positive: int<32, u>; // 32-bit unsigned integersigned: int<32, s>; // 32-bit signed integer (explicit)Signedness options:
s- Signed (can represent negative numbers)u- Unsigned (only non-negative numbers)
Integer Literal Formats
Section titled “Integer Literal Formats”Quant supports multiple number bases:
decimal: int = 42; // Decimal (base 10)hex: int = 0x2A; // Hexadecimal (base 16)octal: int = 0o52; // Octal (base 8)binary: int = 0b101010; // Binary (base 2)Float Types
Section titled “Float Types”Floating-point types for decimal numbers.
f1: float; // 64-bit float by defaultf2: float<32>; // 32-bit floatf3: float<64>; // 64-bit float (explicit)Float literal:
pi: float = 3.14159;e: float<32> = 2.71828;Boolean Type
Section titled “Boolean Type”Boolean type for true/false values.
flag: bool = True;is_valid: bool = False;Boolean values:
TrueFalse
String Type
Section titled “String Type”String type for text data.
message: str = "Hello, World!";name: str = "Quant";String literals use double quotes (") and support escape sequences:
\n- Newline\t- Tab\"- Quote\\- Backslash
Character Type
Section titled “Character Type”Character type for single characters.
ch: char = 'A';newline: char = '\n';Character literals use single quotes (').
Void Type
Section titled “Void Type”The void type indicates no value.
fn print_message() -> void { print("This function returns nothing\n");}Composite Types
Section titled “Composite Types”Array Types
Section titled “Array Types”Arrays are declared using square brackets around the element type.
One-Dimensional Arrays
Section titled “One-Dimensional Arrays”numbers: [int]; // Array of integersvalues: [float]; // Array of floatsflags: [bool]; // Array of booleansMulti-Dimensional Arrays
Section titled “Multi-Dimensional Arrays”matrix: [[int]]; // 2D array (array of arrays)cube: [[[int]]]; // 3D arrayArrays with Const Elements
Section titled “Arrays with Const Elements”constants: [const int]; // Array of immutable integersmatrix: [[const int]]; // 2D array of immutable integersSee the Arrays Guide for more details on array usage.
Function Types
Section titled “Function Types”Functions are declared with parameter types and a return type.
fn add(a: int, b: int) -> int { return a + b;}
fn print_message(msg: str) -> void { print("%s\n", msg);}
fn process() -> float { return 3.14;}Function signature syntax:
fn name(param1: type1, param2: type2, ...) -> return_type { // body}Type Qualifiers
Section titled “Type Qualifiers”Const Qualifier
Section titled “Const Qualifier”The const qualifier makes a type immutable after initialization.
pi: const float = 3.14159;max_size: const int = 100;
// pi = 3.14; // Error: cannot modify const variableConst in Types
Section titled “Const in Types”values: [const int]; // Array of const integersdata: [[const float]]; // 2D array of const floatsQualified Types
Section titled “Qualified Types”A qualified type combines a base type with optional qualifiers:
x: int; // Plain typey: const int; // Qualified with constz: [const int]; // Array type with const elementsType Inference
Section titled “Type Inference”While Quant requires explicit type annotations for variable declarations, it can infer types in certain contexts:
In Function Calls
Section titled “In Function Calls”fn calculate() -> int { return 42;}
fn main() -> int { x: int = calculate(); // Type of calculate() is inferred return 0;}In Expressions
Section titled “In Expressions”fn main() -> int { x: int = 10; y: int = 20; z: int = x + y; // Type of (x + y) is inferred return 0;}Type Casting
Section titled “Type Casting”Explicit type conversion using the target type as a function:
fn main() -> int { // Float to int x: float = 3.14; y: int = int(x); // y = 3
// Int to float a: int = 42; b: float = float(a); // b = 42.0
// In expressions c: int = int(3.9) + int(2.1); // c = 5
return 0;}Type Compatibility
Section titled “Type Compatibility”Assignment Compatibility
Section titled “Assignment Compatibility”- Types must match exactly for assignment
- No implicit conversions (must use explicit casting)
x: int = 42;y: float = float(x); // OK: explicit cast// z: float = x; // Error: type mismatchParameter Compatibility
Section titled “Parameter Compatibility”Function arguments must match parameter types exactly:
fn add(a: int, b: int) -> int { return a + b;}
fn main() -> int { x: int = 10; y: int = 20; result: int = add(x, y); // OK: types match
// z: float = 5.0; // add(x, z); // Error: float doesn't match int parameter
return 0;}Type Sizes and Ranges
Section titled “Type Sizes and Ranges”Integer Ranges
Section titled “Integer Ranges”Integer value ranges depend on bit size and signedness:
| Type | Bits | Signed Range | Unsigned Range |
|---|---|---|---|
int<8, s> | 8 | -128 to 127 | N/A |
int<8, u> | 8 | N/A | 0 to 255 |
int<16, s> | 16 | -32,768 to 32,767 | N/A |
int<16, u> | 16 | N/A | 0 to 65,535 |
int (default) | 32 | -2,147,483,648 to 2,147,483,647 | N/A |
int<32, u> | 32 | N/A | 0 to 4,294,967,295 |
int<64, s> | 64 | -(2^63) to (2^63)-1 | N/A |
int<64, u> | 64 | N/A | 0 to (2^64)-1 |
Float Precision
Section titled “Float Precision”| Type | Bits | Precision |
|---|---|---|
float<32> | 32 | ~7 decimal digits |
float (default) | 64 | ~15 decimal digits |