Skip to content

Add user-defined iterator protocol #123

Description

@itsfuad

Goal

Add simple structural custom iteration while preserving built-in range, array, and slice loops.

Contract

  • Accessible Next() method with exactly receiver and no extra/defaulted parameters.
  • Return ?Item; outer none ends loop and a present result binds one Item.
  • One custom item binding: for item in source. Built-in index, value behavior stays unchanged.
  • Concrete static dispatch only. No Iterator interface, generic constraint, boxing, automatic erasure, iterator vtable, free-function fallback, or tuple prerequisite.
  • Source evaluates once. continue attempts one next call; break and return do not advance again.
  • Canonical call, optional-flow, CFG/effects, ownership, provenance, and cleanup guards remain authoritative.

Implementation status

For-side implementation complete, approved, committed, and pushed on feature/structural-iterators. Awaiting PR/merge lifecycle.

Commits:

  • d614336 Add static iteration over local struct cursors
  • e088415 Support temporary struct iterator sources
  • 6c4e39d Generalize structural iterator sources and items

Final behavior:

  • Removed concrete-struct, source-syntax, and scalar-item whitelists.
  • Existing places retain storage identity; produced values use loop-scoped ordinary bindings.
  • Dynamic indexed places capture one shared or mutable reference according to receiver shape, so index expressions evaluate once and body mutation cannot retarget traversal.
  • Aggregate, owned-pointer, nested-optional, and legal reference items use ordinary binding, move, provenance, and cleanup machinery.
  • Exact semantic payload-depth evidence removes one outer optional layer without flattening optional Item.
  • Checked expansion remains visible before CFG, flow, effects, definite-init, ownership, HIR, and MIR. Lowering performs no method rediscovery or hidden iterator operation.
  • Obsolete staged-limit diagnostics removed; protocol errors remain source-facing and actionable.
  • Language specification and LSP item hover coverage added.

Coverage

  • Differential implicit/explicit checks for field, dynamic-index, reference-parameter sources and aggregate, owner, nested-optional, and reference items.
  • Existing differential ownership checks cover receiver borrowing, consumption, moves, body access, and conflicting borrows.
  • Runtime fixture covers source evaluation/identity and generalized item behavior.
  • Existing factory lifecycle tests cover empty loops, continue, break, return, nested exits, temporary cleanup, and local resume.
  • Negative fixtures retain signature, binding-shape, mutability, interface-erasure, move, and ordinary-guard failures.

Validation

  • Focused typechecker, ownership, effect, definite-init, CFG, HIR, MIR, and LSP tests passed.
  • go test ./... passed.
  • go run ./scripts/bundle.go passed.
  • Full bundled fixture suite passed: PEEPER_BIN=/home/itsfuad/Dev/Peeper/compiler/build/bin/peeper go test -count=1 ./x_test.
  • gofmt and git diff --check passed.

Separate central-language boundaries

Cross-module method discovery and broader reference-storage/provenance capabilities belong to ordinary method lookup and ownership systems. Future improvements there flow through this checked expansion; no iterator-specific workaround is planned.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlanguage-modelPeeper language model, ownership, pointer, optional, array/slice work

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions