Skip to content

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

TypeSizeDescriptionExample
i3232-bitSigned integer42, -1, 0
f3232-bitSingle-precision float3.14, 2.0
f6464-bitDouble-precision float3.14159265358979
bool1-bitBooleantrue, false
StringpointerUTF-8 string (heap-allocated)"hello"
ptrpointerRaw pointer__thg_ptr_null()
void0-bitNo valueUsed in extern func return types

Type Inference

Thagore infers types from initializer expressions:

inference.tg
let x = 42 # i32 (integer literal)
let y = 3.14 # f32 (float literal)
let name = "Alice" # String
let flag = true # bool

Explicit Type Annotations

Types are explicitly annotated in:

Function Parameters

func add(a: i32, b: i32) -> i32:
return a + b

Function Return Types

func greet(name: String) -> String:
return "Hello, " + name

Struct Fields

struct Point:
x: f32
y: f32

Extern Function Declarations

extern func strlen(s: String) -> i32
extern func sqrtf(x: f32) -> f32

Composite Types

Structs

User-defined composite types with named fields:

struct Rect:
width: i32
height: i32
# Constructor call
let 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)
None

Type 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 CodeCondition
E_TYPE_MATCH_MISSING_ENUMmatch used without any enum declaration
E_TYPE_RANGE_HEADERMalformed for i in a..b: syntax
E_TYPE_IF_EXPRMalformed if-expression
E_TYPE_CLOSUREMalformed closure expression

Phase 3: Constraint Validation

Run semantic passes and intent validation:

Error CodeCondition
E_TYPE_TRAIT_CONSTRAINTTrait/impl/generic-bound semantic check failed
E_TYPE_INTENT_GOALUnsupported intent goal
E_TYPE_PIPELINE_VALIDATEGeneral 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 = f32
type Name = String
type Callback = ptr

Return Type Validation

Functions must have an explicit return type annotation (after ->):

# Explicit return type
func compute(x: i32) -> i32:
return x * 2
# Void return (for extern functions only)
extern func puts(text: String) -> i32
extern func free(p: ptr) -> void