Skip to content

Generic Types Support — Phase 1: Classes & Functions#83

Open
cs01 wants to merge 6 commits intomainfrom
worktree-generics
Open

Generic Types Support — Phase 1: Classes & Functions#83
cs01 wants to merge 6 commits intomainfrom
worktree-generics

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 3, 2026

Generic Types Support — Phase 1: Classes & Functions

Adds comprehensive support for generic classes and functions with type erasure-based implementation.

Overview

Implements Phase 1 of generics support:

  • Generic Classes: class Stack<T>, class Box<T>, class Pair<A,B> with type parameter substitution
  • Generic Functions: Type parameter inference for return types in global and local function scopes
  • Type Argument Checking: Compile-time validation for interface<T> and class<T> type arguments
  • Error Detection: Compile error for generic method returns without type annotations

How It Works

Generic Classes

Uses type erasure: Generic type parameters (T) are compiled to i8* (opaque pointers), and generic arrays (T[]) to %ObjectArray*. No monomorphization — single compiled code for all type instantiations.

class Stack<T> {
  items: T[] = [];
  push(x: T) { this.items.push(x); }
  pop(): T { return this.items.pop(); }
}

const s = new Stack<string>();
s.push("hello");
const x = s.pop(); // inferred as string

Generic Functions

Type parameters are inferred from return type annotations and usage context.

function identity<T>(x: T): T { return x; }
const s: string = identity("hello");
const n: number = identity(42);

Type Arguments on Interfaces & Classes

Detects and rejects invalid type arguments at compile time:

type Result<T> = { ok: T } | { error: string };
const r: Result<number> = { ok: 42 }; // ✓ valid

Changes

Core Codegen:

  • llvm-generator.ts: Generic class/function handling, type parameter substitution
  • variable-allocator.ts: Allocate correct types for generic parameters based on context
  • method-calls.ts: Guard class instances from array method dispatch

Parser:

  • parser-ts/handlers/declarations.ts, parser-native/transformer.ts: Parse/transform generic syntax
  • ast/types.ts: Add generic metadata to class/interface/function nodes

Collections:

  • array/mutators.ts, array/reorder.ts: Add generateObjectArrayPop, generateObjectArrayShift for generic array handling

Tests:

  • 7 new fixtures in tests/fixtures/generics/:
    • stack.ts, queue.ts, box.ts, pair.ts — generic class examples
    • generic-functions.ts — function type inference
    • generic-class-arg.ts, generic-extends.ts, generic-interface.ts — type argument validation

Limitations & Future Work

Current (Phase 1):

  • ✓ Generic classes with string/interface type parameters
  • ✓ Generic functions with return type inference
  • ✓ Type erasure for all Ti8*

Not Yet (Phase 2-3):

  • ✗ Numeric type parameters (new Stack<number>())
  • ✗ Method returning own generic type (swap(): Pair<B,A>)
  • ✗ Generic constraints (<T extends Comparable>)
  • ✗ Generic spreads (...(T[]))

Testing

All existing tests pass. Self-hosting verified (Stage 0-2 pass).

npm test        # Run unit tests
npm run verify  # Full verification + self-hosting

@cs01 cs01 changed the title add dgenerics add generics Mar 3, 2026
@cs01 cs01 changed the title add generics Generic Types Support — Phase 1: Classes & Functions Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant