Skip to content

Add optional-producing call iteration #123

Description

@itsfuad

Goal

Add concise custom iteration syntax for ordinary calls that return optional values, while preserving built-in range, array, and slice loops.

Contract

  • Custom syntax is for item in producerCall().
  • producerCall() is an ordinary free-function, method, or pipe call.
  • The call must return ?T; a present result binds one item of type T, and none ends iteration.
  • The complete call evaluates on every attempt, including the terminating attempt. This includes its callee, receiver, and arguments.
  • continue starts another producer-call attempt; break and return do not advance again.
  • Custom loops provide one item binding, not an implicit index. Built-in index, value sequence behavior remains unchanged.
  • Bare optional values, function values without a call, and objects with a Next method are not producers unless the method call is written explicitly.
  • Ordinary call dispatch remains authoritative: methods of any name, free functions, pipe calls, default arguments, borrow/move adaptation, effects, provenance, and cleanup all use existing call semantics.
  • No implicit Next protocol, source capture, iterator interface, vtable, boxing, generic constraint, or tuple prerequisite is added.

Architecture

The typechecker expands valid optional-producing call loops into ordinary checked call, optional-test, payload-binding, and loop-control statements before CFG, flow, effects, definite-init, ownership, cleanup, HIR, and MIR. Downstream phases consume the checked expansion and do not rediscover producer calls.

The expansion intentionally keeps the complete producer call inside the loop because ordinary call evaluation occurs once per attempt. Existing call checking owns receiver adaptation, default arguments, move/borrow behavior, reference provenance, effects, and cleanup.

Coverage

  • Free-function, method, and pipe producers.
  • Scalar, aggregate, owned-pointer, nested-optional, and legal reference items.
  • Receiver borrowing, consumption, moves, body access, conflicting borrows, continue, break, return, empty loops, and nested exits.
  • Negative coverage for non-optional calls, bare optionals, function values, index bindings, invalid arguments, move/borrow errors, and unsupported item shapes.
  • Language specification, LSP hover coverage, semantic phase tests, and bundled runtime fixtures.

Validation

  • Focused typechecker, ownership, effect, definite-init, CFG, HIR, MIR, and LSP tests.
  • go test ./...
  • go test -race ./...
  • go run ./scripts/bundle.go
  • PEEPER_BIN=$PWD/build/bin/peeper go test -count=1 ./x_test
  • gofmt and git diff --check

Closes #123

Activity

  1. added
    enhancementNew feature or request
    language-modelPeeper language model, ownership, pointer, optional, array/slice work
    on Aug 31, 2026
  2. itsfuad commented on Sep 8, 2026

    @itsfuad
    MemberAuthor

    Architecture planning update (no implementation): traced canonical method calls, optional extraction, CFG/effects, ownership and HIR/MIR. Proposed topology is once-only source initialization -> header checked advancement/result binding -> optional presence branch -> payload binding/body -> latch back to header. Generated operations must have indexed nodes/scopes and real CFG analysis sites before ownership; current loop segments lack those sites. HIR/MIR must consume the same operations without double execution. Reuse existing call adaptation, payload proofs, return origins and cleanup. Include nested optional items in coverage.

    Item-only custom binding is working default pending final review; builtin index,value unchanged.

    New design question: lowercase next is private under current uppercase export convention, and declared method lookup searches current module method table. Cross-module method visibility must be resolved centrally. Exported Next is a recommendation for review, not yet an approved spelling change. No private-method visibility bypass for iteration.

  3. moved this from Todo to In Progress in Peeper Roadmapon Sep 8, 2026
  4. itsfuad commented on Sep 8, 2026

    @itsfuad
    MemberAuthor

    Implementation started on feature/structural-iterators. First slice: local concrete cursors, scalar items, one binding, exported Next() -> ?Item, static calls through central guards. No runtime interface iteration. Generated checked operations will enter CFG/flow/effects/ownership before lowering. Temporary sources, resource-bearing payloads and cross-module method availability remain follow-up scope; no implementation validation claimed yet.

  5. itsfuad commented on Sep 8, 2026

    @itsfuad
    MemberAuthor

    Review refinement: user-facing iterator errors now describe source-level problems and actionable remedies instead of structural/concrete-cursor/scalar terminology. Added Next declaration/return-type labels, optional item/none guidance, argument/default explanations, index advice, local-variable workaround, explicit-loop alternative for unsupported item types, and interface-specific guidance. Recognition and ownership behavior unchanged. Diagnostic tests and negative fixture assert hints and exclude internal wording/hidden names. Validation passed: full Go suite, refreshed bundle, full bundled x_test suite, gofmt and git diff --check. Still uncommitted; first slice remains under user review.

  6. changed the title [-]Add user-defined iterator protocol[/-] [+]Add optional-producing call iteration[/+] on Sep 11, 2026
  7. itsfuad commented on Sep 11, 2026

    @itsfuad
    MemberAuthor

    Documentation correction for the current implementation and PR #135: the feature is optional-producing call iteration, not a Next() protocol.

    Current contract is for item in producerCall() where any ordinary free-function, method, or pipe call returning ?T is valid. The complete call evaluates once per attempt, including the terminating attempt. Next() has no special language meaning; it is valid only as an explicitly written ordinary method call.

    The issue body and PR description now reflect this model. Earlier comments describe superseded design exploration and remain historical.

  8. moved this from In Progress to Done in Peeper Roadmapon Sep 11, 2026
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