Type System
Type System
Thagore is a statically typed language. Types are checked at compile time through the typechecker stage (src/semantics/typecheck/program.tg).
Primitive Types
| Type | Size | Description | Example |
|---|---|---|---|
i32 | 32-bit | Signed integer | 42, -1, 0 |
f32 | 32-bit | Single-precision float | 3.14, 2.0 |
f64 | 64-bit | Double-precision float | 3.14159265358979 |
bool | 1-bit | Boolean | true, false |
String | pointer | UTF-8 string (heap-allocated) | "hello" |
ptr | pointer | Raw pointer | __thg_ptr_null() |
void | 0-bit | No value | Used in extern func return types |
Type Inference
Thagore infers types from initializer expressions:
let x = 42 # i32 (integer literal)let y = 3.14 # f32 (float literal)let name = "Alice" # Stringlet flag = true # boolExplicit Type Annotations
Types are explicitly annotated in:
Function Parameters
func add(a: i32, b: i32) -> i32: return a + bFunction Return Types
func greet(name: String) -> String: return "Hello, " + nameStruct Fields
struct Point: x: f32 y: f32Extern Function Declarations
extern func strlen(s: String) -> i32extern func sqrtf(x: f32) -> f32Composite Types
Structs
User-defined composite types with named fields:
struct Rect: width: i32 height: i32
# Constructor calllet r = Rect(10, 20)Fixed-Size Arrays
func process(arr: [i32; 4]) -> i32: return arr[0] + arr[1]
let data = [1, 2, 3, 4]Enums
Algebraic data types:
enum Option: Some(i32) NoneType Checking Pipeline
The typechecker runs in three phases:
Phase 1: Program Construction
Parse the source and extract all declarations:
- Functions, enums, type aliases
- Feature counters (match, range loop, closures, etc.)
Phase 2: Feature Edge Validation
Check for semantic consistency:
| Error Code | Condition |
|---|---|
E_TYPE_MATCH_MISSING_ENUM | match used without any enum declaration |
E_TYPE_RANGE_HEADER | Malformed for i in a..b: syntax |
E_TYPE_IF_EXPR | Malformed if-expression |
E_TYPE_CLOSURE | Malformed closure expression |
Phase 3: Constraint Validation
Run semantic passes and intent validation:
| Error Code | Condition |
|---|---|
E_TYPE_TRAIT_CONSTRAINT | Trait/impl/generic-bound semantic check failed |
E_TYPE_INTENT_GOAL | Unsupported intent goal |
E_TYPE_PIPELINE_VALIDATE | General lowering validation failure |
ProgramT Type
The typechecker produces a ProgramT struct:
struct ProgramT: source: String normalized: String feature_set: String enums: String aliases: String funcs: String error_count: i32 error_code: String error_message: String # 17 feature counters...Type Aliases
Create type synonyms:
type Distance = f32type Name = Stringtype Callback = ptrReturn Type Validation
Functions must have an explicit return type annotation (after ->):
# Explicit return typefunc compute(x: i32) -> i32: return x * 2
# Void return (for extern functions only)extern func puts(text: String) -> i32extern func free(p: ptr) -> void