Skip to content

Tail Call Optimization (TCO) implementation in compile time #32743

Closed
@MiniMarvin

Description

@MiniMarvin

Search Terms

Functional Programming, ES6, Tail Call Optimization, TCO

Suggestion

Would be pretty nice to add a tail call optimization, once present in V8 for NodeJS 7.x, but later removed for some reasons I don't really understand, but about some other performance issues created in the browser. The idea is to create a transpiler to generate the iterative code for some tail-call optimizations, feature already present in Elm for example.

Use Cases

That would allow typescript people to take advantage of some other resources of pure functions without any real worry with StackOverflow from using the functional programming advantages.
The current approach in Typescript is not to use it, use iterative loops or even model the problem such a way that will never impact the code but is a huge problem for people trying to make the code more functional. There's already some implementation of it in some more functional transpilers for JS like Elm and is pretty useful in order to make functions modeling in an easier way.

Examples

Once a programmer code something like:

function fact (x: number, acc: number): number {
  if (x === 0) {
    return acc;
  }
  else {
    return fact(x - 1, x*acc);
  }
}

The code generated for javascript would be something like:

function (x, acc) {
  while(true) {
    if(!x) {
      return acc;
    }
    else {
      let t1 = x - 1;
      let t2 = acc * x;
      x = t1;
      acc = t2;
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions