elvish
elvish copied to clipboard
grammar: is it by design to force `else` after `{` on same line?
elvish version: 0.11
I found a counterintuitive behavior of condition syntax when using elvish writing my daily script:
Just see the example:
This one works fine, just echoes yay.
if (eq 1 1) {
echo yay
} else { echo wow }
After inserting a new line before else
:
if (eq 1 1) {
echo yay
}
else { echo wow }
elvish outputs:
yay
Exception: exec: "else": executable file not found in $PATH
It looks like else
was treated as a function.
Actually, I want to write something like:
if (eq 1 1) { echo yay }
else { echo wow }
This one throws an exception like the above one. I want to write compact code like this, because I'm writing a git command wrapper, using a big condition clause to process command arguments and call the corresponding function.
I wonder if this grammar is by design and I'm going the wrong way to process arguments(with big condition clause)?
@frantic1048 it is by design, and explained here: https://elvish.io/learn/effective-elvish.html#code-blocks
This is because in Elvish, control structures like if follow the same syntax as normal commands, hence newlines terminate them. To make the code block part of the if command, it must appear on the same line.
(sorry, fixed URL which was originally pasted wrong)
I'm keeping this open until this is documented in the fundamentals doc.
@zzamboni Thanks for the explanation. I didn't notice effective-elvish page before.And I'm always seeking info under https://elvish.io/ref
It seems to me the discussion of code blocks in the effective-elvish document should be split up with parts moved to the "fundamentals" document and parts moved to the language reference. That discussion of code blocks isn't really a style issue in the sense meant by effective elvish style.
Summarizing what needs to be done:
- [ ] Document the syntax of control structures in the "fundamentals" doc (by moving some explanations from Effective Elvish)
- [ ] Produce better error message when using
else
, or any of the special command keywords (likeexcept
mentioned in #987), as a command