From 5cab30a06582853bd2d62a3ef4e22ea7e9dde7ed Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 22 Nov 2026 01:48:59 +0000 Subject: [PATCH] Generator: next(value?: TNext) again - given to an Iterable, the compiler iterates it by an adapter (TypeScriptCompiler #984) Co-Authored-By: Claude Opus 5.5 --- src/types/lib.types.d.ts | 8 +++----- tests/generator_next_return.ts | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/types/lib.types.d.ts b/src/types/lib.types.d.ts index 5bf5f2d..5cb39a1 100644 --- a/src/types/lib.types.d.ts +++ b/src/types/lib.types.d.ts @@ -28,11 +28,9 @@ interface IterableIterator { } interface Generator { - // next() takes no value here: a Generator is given to an Iterable as it is, so their next() must be one - // function type - `(value?: TNext) =>` called as an Iterable's `() =>` was a call of another signature (wasm's - // "function signature mismatch"; natively the yield read a value never given). Not declared here yet: - // next(value) (TSLANG 153). - next: () => IterateResult; + // next(value) is what the `yield` it resumes evaluates to. Given to an Iterable or an Iterator, whose + // next() takes none, a Generator is iterated by a generator over its values (the compiler makes it). + next: (value?: TNext) => IterateResult; // a generator declared to return a Generator has them (-mm=own: return() only for a T that owns nothing, a // number or a boolean); one declared otherwise, or not at all, is a Generator without them return?: (value: T) => IterateResult; diff --git a/tests/generator_next_return.ts b/tests/generator_next_return.ts index 69693b9..3f63c0c 100644 --- a/tests/generator_next_return.ts +++ b/tests/generator_next_return.ts @@ -1,9 +1,22 @@ -// Generator: return(value) +// Generator: next(value) is what the `yield` it resumes evaluates to, return(value) // finishes a paused generator (its finally blocks run), throw(e) throws at its yield. An // array given to an Iterable is iterated (under -mm=own its copy). let cleaned = 0; +function* running(): Generator { + let total: number = 0; + while (true) { + const v = yield total; + total += v; + } +} + +function* sent(): Generator { + const a = yield 1; + yield a === undefined ? 2 : 3; +} + function* guarded(): Generator { try { yield 1; @@ -38,6 +51,16 @@ function sum(values: Iterable) { } function main() { + const r = running(); + r.next(); + r.next(2); + r.next(3.5); + const t = r.next(4); + assert(t.value == 9.5 && !t.done, "next(value)"); + + // a Generator given to an Iterable: iterated, its yields given no value + assert(sum(sent()) == 3 || sum(sent()) == 4, "a Generator with a TNext as an Iterable"); + const g = guarded(); assert(g.next().value == 1, "first"); const f = g.return(7);