Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constrain return statements in generator functions. #18726

Closed
brandonbloom opened this issue Sep 24, 2017 · 4 comments
Closed

Constrain return statements in generator functions. #18726

brandonbloom opened this issue Sep 24, 2017 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@brandonbloom
Copy link
Contributor

brandonbloom commented Sep 24, 2017

Generator functions should constrain their return types to match the result type of the return method on their declared Iterator subtype.

This is deeply related to #2983 - but I wanted to call it out separately because it's a small subset of the total changes required to completely support iterators as coroutines.

TypeScript Version: 2.5.2

Code

interface Iterator<T> {
    next(value?: any): IteratorResult<T>;
    return?(value?: any): IteratorResult<any>; // changed to represent current reality.
    throw?(e?: any): IteratorResult<T>;
}

type Effect = { op: string };

interface Coroutine extends Iterator<Effect> {
    // Override return to constrain return values to Effect.
    return?(value?: any): IteratorResult<Effect>;
}

interface Process extends Coroutine {
    [Symbol.iterator](): Coroutine;
}


let f = function* (): Process {
    yield { op: 'tick' }; // OK.
    yield 1; // Expected error: Not an effect.
    return 1; // Unexpected lack of error! Not an Effect.
}
brandonbloom added a commit to brandonbloom/TypeScript that referenced this issue Sep 24, 2017
@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Sep 25, 2017
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed In Discussion Not yet reached consensus labels Oct 10, 2017
@agentcooper
Copy link
Contributor

@mhegazy @RyanCavanaugh @rbuckton can you please take a look at #23571?

@falsandtru
Copy link
Contributor

I think generator function's return type is not constrained by iterator type. At least that return type can be undefined.

@mhegazy
Copy link
Contributor

mhegazy commented May 14, 2018

I do not think this is the correct solution to this issues. the underlying issue here is that the generator has two possible return states, and each has a possibly different type. each type is witnessed in different use case (done: true vs done: false). The solution thus is not to restrict return to fit the yielded type, but rather to correctly model the generator interface.

#11375 tracks correctly modelling the generator interface and correctly inferring the the three type, 1. yielded values, 2, returned values, and 3. yield expression type (input to next).

@mhegazy
Copy link
Contributor

mhegazy commented May 14, 2018

closing in favor of #11375.

@mhegazy mhegazy closed this as completed May 14, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants