Yuescript
Yuescript copied to clipboard
Accepted values in backcall/pipeline operator.
Hello. While trying to move one of my projects over from Moonscript to MoonPlus, I tried to do the following:
readFile "example.txt"
|> (_) -> extract _, language, {}
|> (_) -> parse _, language
|> emit
|> render
|> print
But I was met with the following error:
Fail to compile: fir/generic/emit/markdown.mp.
193: backcall operator must be followed by chain value
|> (_) -> extract _, language, {}
^
In this case, some questions or proposals come to mind:
- What counts as a "chain value"?
- Could a function be supported in a future update, as in my snippet?
- Could there be a simpler way to do what I'm trying to do? Something along the lines of
|> extract $, language, {}or|> extract @, language, {}. - Although this isn't a serious complain, the
Fail to compileerror does not roll off the tongue very well, maybe something likeCould not compileorFailed to compile?
- A chain value for backcall syntax could be one of the followings:
Callable:
@, @field, (a + b), func, t.func, t\func, t.func?, $macro
Invoke:
f!, f(1,2,3), $macro!, $macro(1,2,3)
DotChainItem:
with t
1 |> .func
ColonChainItem:
with t
1 |> \func
- The accepted form for your example could be
readFile("example.txt")
|> ((_)-> extract _, language, {})
|> ((_)-> parse _, language)
|> emit
|> render
|> print
-- or
readFile("example.txt")
|> extract(language, {})
|> parse(language)
|> emit
|> render
|> print
The back call operator has a higher operator precedence. Without brackets the codes would be compiled as:
-- the example without proper brackets
readFile "example.txt"
|> extract language, {}
|> parse language
|> emit
|> render
|> print
-- is treated like
readFile ("example.txt" |> extract language, ({} |> parse (language |> emit |> render |> print)))
- It is really a problem. Got it fixed. 6a48f108666126893dcf7ff54b12172795858d3a
Thank you for the help!
Just found that I missed a question. There is a syntax for your need:
readFile("example.txt")
|> extract(_, language, {}) -- _ is a placeholder here instead of a variable indicating where to insert the argument
|> parse(_, language)
|> emit
|> render
|> print
2 |> func(1, _, 3) -- will be compiled to func(1, 2, 3)
Changed the backcall operator syntax.
readFile "example.txt"
|> extract language, {}
|> parse language
|> emit
|> render
|> print
will be compiled to the expected output with latest commit, extra parentheses are no longer needed.
return print(render(emit(parse(extract(readFile("example.txt"), language, { }), language))))