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

Return type in generator functions #10331

Closed
d180cf opened this issue Aug 15, 2016 · 1 comment
Closed

Return type in generator functions #10331

d180cf opened this issue Aug 15, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@d180cf
Copy link

d180cf commented Aug 15, 2016

Consider the following generator:

function* G() {
  yield "a";
  yield "b";
  yield "c";
  return 123;
}

The return type of G will be Iterable<string> that does not reflect the type of the final value, so here the variable will be of type any:

const res = yield* G();

This matters when yield is used not to yield some useful values, but to suspend/resume the function. For instance, a depth-first search function can make use of this trick:

// not sure if this is allowed in tsc... the idea is
// that node.value is the value and node[0], node[1], ...
// are subnodes (aka "children")
interface Node<T> extends Iterable<Node<T>> {
  value: T;
}

function* search<T>(node: Node<T>, test: (node: Node<T>) => boolean) {
  if (test(node))
    return node; // see, the actual return type is Node

  for (const subnode of node) {
    // and here is the trick: in the debug mode this yield
    // suspends the search and gives control to UI, so it
    // can render the current state of the search and give
    // and option to continue or stop the search; while in
    // in the release mode (debug=false) this yield is skipped
    // and the performance of search(...) is the same as if it
    // was written as a plain recursive function
    debug && (yield "going to " + subnode.value + "...");

    // search(...) returns Iterable<any>, so result is any here
    const result = yield* search(subnode, test);

    if (result)
      return result;
  }

  return null; // node not found
}
@yortus
Copy link
Contributor

yortus commented Aug 15, 2016

#2983 list some steps toward achieving this. The boolean literals are in master, but I don't believe any of the generator-specific changes have been made yet. Once generators have their TReturn type inferred, then hopefully yield* expressions can be better typed.

Not sure why that issue is closed, given that most of the work described there has not been implemented.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Aug 15, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants