From 4bbf03fdedf22d3eb94e9513ba1192992fefb072 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 23:33:25 +0000 Subject: [PATCH] Generator: next(value?), return?() and throw?(); own object_dictionary test lifted; tests for next(value), return(), throw() and arrays/strings as an Iterable Needs a compiler with TypeScriptCompiler #982 and #983. Co-Authored-By: Claude Opus 5.5 --- src/types/lib.types.d.ts | 16 +++++--- tests/generator_next_return.ts | 75 ++++++++++++++++++++++++++++++++++ tests/iterable_string.ts | 19 +++++++++ tests/object_dictionary.ts | 1 - 4 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 tests/generator_next_return.ts create mode 100644 tests/iterable_string.ts diff --git a/src/types/lib.types.d.ts b/src/types/lib.types.d.ts index 78749ae..7123901 100644 --- a/src/types/lib.types.d.ts +++ b/src/types/lib.types.d.ts @@ -13,10 +13,12 @@ interface ClassIterator { // TypeScript's iteration types, as far as a generator here can be one. A generator function returns // an object with next(), and for...of takes any object with next(), so here an Iterable is such an // object too (not one with [Symbol.iterator]): `function* g(): Generator`, and a generator -// given to an Iterable or Iterator parameter, work. TReturn and TNext are taken but not used: -// next() takes no value, and a returned value is one more value. IteratorResult is an alias, not an -// interface, so a generator's next() matches it. -// Not yet (compiler): an array or a string as an Iterable, next(value), return() and throw(). +// given to an Iterable or Iterator parameter, work, and so does an array or a string given to an +// Iterable. next(value) of a Generator is what the `yield` it resumes evaluates +// to; a generator whose next() takes no value is a Generator too. A returned value of a type other +// than T is an error. return(value) and throw(e) resume a paused generator finishing (its finally blocks +// run) or throwing at its yield. IteratorResult is an alias, not an interface, so a generator's next() +// matches it. type IteratorResult = IterateResult; interface Iterable { @@ -28,7 +30,11 @@ interface IterableIterator { } interface Generator { - next: () => IterateResult; + 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; + throw?: (e: any) => IterateResult; } // Promise is T: an async generator's next() gives the result itself diff --git a/tests/generator_next_return.ts b/tests/generator_next_return.ts new file mode 100644 index 0000000..63cb051 --- /dev/null +++ b/tests/generator_next_return.ts @@ -0,0 +1,75 @@ +// 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* guarded(): Generator { + try { + yield 1; + yield 2; + yield 3; + } finally { + cleaned++; + } +} + +function* caught(): Generator { + let stopped = false; + try { + yield 1; + yield 2; + } catch (e) { + stopped = true; + } + + if (stopped) { + yield 100; + } +} + +function sum(values: Iterable) { + let s: number = 0; + for (const v of values) { + s += v; + } + + return s; +} + +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)"); + + const g = guarded(); + assert(g.next().value == 1, "first"); + const f = g.return(7); + assert(f.done && f.value == 7, "return(value)"); + assert(cleaned == 1, "finally ran on return()"); + assert(g.next().done, "finished after return()"); + + const c = caught(); + c.next(); + const h = c.throw("stop"); + assert(h.value == 100 && !h.done, "throw(e) caught at the yield"); + assert(c.next().done, "finished after throw()"); + + const a = [1, 2, 3.5]; + assert(sum(a) == 6.5, "an array as an Iterable"); + a.push(4); + assert(sum(a) == 10.5, "the array still the caller's"); + + print("ALL DONE"); +} diff --git a/tests/iterable_string.ts b/tests/iterable_string.ts new file mode 100644 index 0000000..a9c4e3b --- /dev/null +++ b/tests/iterable_string.ts @@ -0,0 +1,19 @@ +// not own: a for...of over an Iterable is refused under -mm=own (a compiler limit, TSLANG 153) +// A string given to an Iterable parameter is iterated by its characters. + +function count(values: Iterable) { + let n = 0; + let s = ""; + for (const c of values) { + n++; + s = c + s; + } + + assert(s == "olleh", "characters in order"); + return n; +} + +function main() { + assert(count("hello") == 5, "a string as an Iterable"); + print("ALL DONE"); +} diff --git a/tests/object_dictionary.ts b/tests/object_dictionary.ts index 305f750..fbd5fc3 100644 --- a/tests/object_dictionary.ts +++ b/tests/object_dictionary.ts @@ -1,4 +1,3 @@ -// not own: until a released compiler has the fix for a parameter moved on paths that exclude each other (set) // ObjectDictionary: the value of a string index signature, `{ [key: string]: T }`. Its entries by keys // known at run time: written, overwritten, deleted by writing undefined, and an absent key reads undefined. class P {