Repository navigation
next(value) of a Generator<T, TReturn, TNext>; an object method of another arity fills an interface member - #974
Merged
Merged
Conversation
…other arity fills an interface member A generator typed Generator<T, TReturn, TNext> gets next(value?: TNext), and the `yield` it resumes evaluates to the value (TNext's default when none is given). An object-literal method taking fewer parameters than the interface's member, or more optional ones, is called through a thunk giving it as many arguments as it takes, so a generator whose next() takes no value is a Generator whose next takes one, and the other way round. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…at stays open Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #973 (TSLANG 153).
An object method of another arity fills an interface member. An object-literal method given as an interface's function member that takes fewer parameters (
next()fornext: (value?: number) => R, the rest ignored as JavaScript does), or more optional ones (next(v?)fornext: () => R), was refused: "can't assign '() => ...' to '(p1: number | undefined) => ...': it takes 0 argument(s), 2 expected". The vtable slot now holds a thunk that gives the method as many arguments as it takes (isArityAdaptableObjectMethod,generateSynthMethodToCallField), the same way as for a field holding a closure.next(value) of a
Generator<T, TReturn, TNext>. The generator's next() takesvalue?: TNext, and theyieldit resumes evaluates to that value as a TNext. When next() is given no value (the first call, a for...of), theyieldgives TNext's default. It used to give undefined. A generator whose next() takes no value can still be used as such a Generator, and the other way round.The default library's
Generatorshould declarenext: (value?: TNext) => ...once a compiler with this change is released; a released compiler without it can't treat a plain generator as one. With that declaration patched in locally, the library'stests/iteration_types.tspasses under gc, rc and own.Still open (TSLANG 153): return()/throw(); under -mm=own, sending an object through next() (next() keeps the object it is given).
Tests:
00object_method_arity.ts,00generator_next_value.ts(compile, JIT, rc, own). Full ctest locally: 8776 run, 0 failed.🤖 Generated with Claude Code