gracelang / language

Design of the Grace language and its libraries
GNU General Public License v2.0
6 stars 1 forks source link

Can two blocks be opened on the same line? #180

Closed IsaacOscar closed 1 year ago

IsaacOscar commented 5 years ago

Consider the following pointless code:

def x = if (true) then {{
   print("true")
}} else {{
   print("false")
}}
x.apply

I have in fact tried to write code like that in grace (where an if statement returns a block).

In minigrace this produces an error:

Syntax error: the prior line opened two blocks. There is no way for you to close them correctly! Please split the prior line into two.

However, in kernan it runs as expected.

From the spec, I see two relavent rules (emphasis mine)

  1. If a line contains an unmatched opening brace, that line is said to open a code block. All lines up to the matching closing brace comprise the body of the code block, and must be indented more than the line containing the opening brace.
  2. If the closing brace that closes the code block is the first non-space character on a line, then the indentation of the closing brace must be the same as that of the line containing the matching opening brace. Otherwise, the line containing the closing brace must be indented like all the other lines in the code block.

It seems in the line }} else {{, the first closing brace satisfies the first part of rule 4, however the second one contradicts the second part, since the line it is on is not indendented enough. I suggest deleting the "otherwise" clause from rule 4, and adding the folowing bolded text to rule 3.

... All lines up to the matching closing brace comprise the body of the code block, and if not begining with a closing brace must be indented more than the line containing the opening brace.

Or if you think that my example should be rejected (why?) add a clarfication to the spec that this is intended; because it's quite subtle why this code violates the rules.

apblack commented 1 year ago

I think that the example should be rejected because a double opening brace is more likely to be a typo than to be deliberate. A block containing nothing but a block is very occasionally useful (I’ve used it once), but can be accommodated with more clarity using extra lines and extra indentation.

If others disagree and we decide to change the indentation rules, then we need a better change than the one proposed above, because the proposed change leaves the “otherwise” situation undefined.

apblack commented 1 year ago

I believe that the current indentation rules are fine. In the more than three years that this issue has been open, no one has proposed better rules. I have no problem with adding a sentence to the spec highlighting that this is deliberate, but don't think that it warrants too much ink.

apblack commented 1 year ago

Spec updated with counter-example in commit 2a83d19