Skip to content

add generic class and function support#83

Merged
cs01 merged 10 commits intomainfrom
worktree-generics
Mar 4, 2026
Merged

add generic class and function support#83
cs01 merged 10 commits intomainfrom
worktree-generics

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 3, 2026

Summary

Adds generic class and function support via type erasure (Ti8*). No monomorphization — all type parameters erase to opaque pointers at the LLVM IR level, keeping codegen simple and self-hosting compatible.

Generic classes:

  • class Stack<T>, Box<T>, Queue<T>, Pair<A, B>, Wrapper<T extends Printable> all work
  • T[] erases to %ObjectArray*; push/pop/shift guarded against generic class instances to avoid dispatching to built-in array handlers
  • new Stack<Point>(), new Box<Animal>() — interface and class type args supported
  • type Point = { ... } type alias works as a generic type arg (treated as interface)

Generic functions:

  • identity<T>, first<T>(arr: T[]) — type parameter return types infer correctly for both locals and globals
  • Compile-time error emitted when generic method return is used without a type annotation

Self-hosting fixes:

  • Removed block scope { } from case "type_alias_declaration" in native transformer — the native compiler's break inside a block scope exited the block rather than the switch, silently dropping all type alias declarations from the AST
  • Fixed FunctionNode struct layout crash in native compiler
  • Moved typeParameters to end of interfaces (CLAUDE.md rule improve install scripts #3); refactored union-type-checker

Tests: 9 new fixtures in tests/fixtures/generics/ — all pass with both node and native compilers. Stage 0 and Stage 1 self-hosting pass (npm run verify:quick).

Limitations (not in this PR)

  • Numeric type params (new Stack<number>()) not supported — double and i8* have different sizes
  • Generic method returning its own swapped type params (e.g. swap(): Pair<B,A>) — result not tracked as class instance
  • No generic constraints beyond single extends clause

Test plan

  • npm test — 396+ tests pass (5 pre-existing network failures unrelated to generics)
  • All 9 generics fixtures pass: stack, queue, box, pair, generic-functions, generic-interface, generic-type-alias, generic-class-arg, generic-extends
  • npm run verify:quick — Stage 0 and Stage 1 self-hosting pass

@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
@cs01 cs01 force-pushed the worktree-generics branch from 16de1a9 to 9b9d5a7 Compare March 4, 2026 16:01
@cs01 cs01 changed the title Generic Types Support — Phase 1: Classes & Functions add generic class and function support Mar 4, 2026
@cs01 cs01 merged commit 1c0875e into main Mar 4, 2026
12 checks passed
@cs01 cs01 deleted the worktree-generics branch March 4, 2026 16:27
@cs01 cs01 restored the worktree-generics branch March 4, 2026 20:01
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