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

Feature request: Conditional function chaining #1109

Open
ceremcem opened this issue Aug 5, 2020 · 1 comment
Open

Feature request: Conditional function chaining #1109

ceremcem opened this issue Aug 5, 2020 · 1 comment

Comments

@ceremcem
Copy link

ceremcem commented Aug 5, 2020

I think it would be nice to have conditional function chaining, like the following:

a
  .x!
  .y! if foo
  .z!

...which will be compiled to:

var x$;
x$ = a;
x$ = x$.x();
if (foo) {
  x$ = x$.y();
}
x$ = x$.z();

This feature will be useful when we need a conditional middle step on a long function chain.

@anko
Copy link

anko commented Aug 5, 2020

For comparison, currently possible patterns:

  1. Cascade with conditional:

    a
      ..x!
        (if foo then ..y! else ..)
          ..z!
    var x$, y$, z$;
    x$ = a;
    y$ = x$.x();
    z$ = foo ? y$.y() : y$;
    z$.z();
  2. Pipe-chaining accessor functions:

    a
      |> (.x!)
      |> -> if foo then it.y! else it
      |> (.z!)
    (function(it){
      return it.z();
    })(
    function(it){
      if (foo) {
        return it.y();
      } else {
        return it;
      }
    }(
    function(it){
      return it.x();
    }(
    a)));

The suggested syntax would be clearer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants