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

Pipe operator |> syntax before 1.0 release #331

Open
bnyu opened this issue Nov 19, 2024 · 0 comments
Open

Pipe operator |> syntax before 1.0 release #331

bnyu opened this issue Nov 19, 2024 · 0 comments

Comments

@bnyu
Copy link

bnyu commented Nov 19, 2024

MoonBit provides a convenient pipe operator |>, which can be used to chain regular function calls:

fn init {
  x |> f // equivalent to f(x)
  x |> f(y) // equivalent to f(x, y)

  // Chain calls at multiple lines
  arg_val
  |> f1 // equivalent to f1(arg_val)
  |> f2(other_args) // equivalent to f2(f1(arg_val), other_args)
}

This is from Moonbit doc, it is cool. But the syntax is not intuitive for multi-parameter functions, because the second argument is written in the first position, this is inconsistent with directly function call. (method call has an implicit receiver parameter which is consistent with function signature, but the pipe syntactic sugar is completely different)

So I suggest we use _ placeholder syntax maybe like this x |> f(_, y) or x |> f($, y)
This also makes we can pipe to second or third parameter not only first:

fn init {
  debug(3.14 |> Double::to_int) // output: 3
  debug(4 |> Array::make(_, 'c')) // output: ['c', 'c', 'c', 'c']
  debug('c' |> Array::make(4, _)) // output: ['c', 'c', 'c', 'c']
}

Originally posted by @bnyu in #299 (comment)

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

1 participant