Skip to content

Add typed list[T] with Python-style list literals #33

@synapticvoid

Description

@synapticvoid

Summary

Introduce a built-in list[T] type with Python-like literals ([]) and a minimal, type-safe method set.

Proposed Syntax

Type syntax

  • list[T]

Literal syntax

  • Empty list: []
  • Non-empty list: [a, b, c]

Method syntax

  • xs.len() -> Int
  • xs.contains(value: T) -> Bool
  • xs.get(index: Int) -> IndexError!T
  • xs.add(value: T) -> Unit (mutating)
  • xs.remove(index: Int) -> IndexError!T (mutating, remove-at-index)

Type Inference Rules

  • const xs = [1, 2, 3] infers list[int]
  • Empty list literals require annotation:
    • const xs: list[int] = []
    • var ys: list[str] = []

Type Safety Rules

  • All list elements must be assignable to T
  • Mixed-type literals are invalid unless explicitly typed as list[any]
  • No implicit any inference from list literals

Mutability Rule

  • const forbids rebinding
  • Mutating methods (add, remove) require a mutable receiver (var)
  • Example:
    • Invalid: const xs = [1, 2]; xs.add(3);
    • Valid: var xs = [1, 2]; xs.add(3);

Scope (Minimal)

  • Parser support for:
    • list[T] in type positions
    • list literals: [], [x, y]
    • method calls on list values (existing call/field syntax)
  • Type checker support for:
    • inference for non-empty list literals
    • element type validation
    • explicit-type requirement for empty list literals
    • list method signatures and mutable-receiver checks
  • Interpreter support for:
    • list runtime value
    • method dispatch/intrinsics for len, contains, get, add, remove

Out of Scope

  • User-defined generics
  • Full list method surface area
  • Slices/ranges syntax
  • Stack API (push/pop) — reserved for future stack type

Acceptance Criteria

  • Valid:
    • const xs: list[int] = [1, 2, 3]
    • var xs = [1, 2, 3]; xs.add(4);
    • const n: Int = [1, 2, 3].len();
  • Invalid (type errors):
    • const xs: list[int] = [1, "x"]
    • const xs = [] (no annotation)
    • const xs = [1, 2]; xs.add(3);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions