elvish icon indicating copy to clipboard operation
elvish copied to clipboard

grammar: is it by design to force `else` after `{` on same line?

Open frantic1048 opened this issue 6 years ago • 5 comments

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 avatar Mar 21 '18 08:03 frantic1048

@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)

zzamboni avatar Mar 21 '18 08:03 zzamboni

I'm keeping this open until this is documented in the fundamentals doc.

xiaq avatar Mar 21 '18 13:03 xiaq

@zzamboni Thanks for the explanation. I didn't notice effective-elvish page before.And I'm always seeking info under https://elvish.io/ref

frantic1048 avatar Mar 21 '18 18:03 frantic1048

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.

krader1961 avatar Jun 27 '20 04:06 krader1961

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 (like except mentioned in #987), as a command

xiaq avatar Feb 28 '22 16:02 xiaq