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

Putting else statement on a new line causes a syntax error #650

Closed
malcolmrjones opened this issue May 8, 2021 · 1 comment · Fixed by #678
Closed

Putting else statement on a new line causes a syntax error #650

malcolmrjones opened this issue May 8, 2021 · 1 comment · Fixed by #678

Comments

@malcolmrjones
Copy link

I was trying out Grain and stumbled upon some strange behavior regarding the placement of the else statement with if statements. It seems that Grain requires the beginning of an else statement to appear immediately after the end of an if block. I know some various coding styles place the beginning of the else statement on a new line. My personal preference is that else statements begin on a new line. I understand that an if statement in Grain requires an else statement but I'm not sure how the else statement starting on a new line is a syntax error. Is this an intentional part of Grain's syntax?

The following code resulted in a syntax error:

// iffy.gr
let number = 21

if (number % 2 == 0) {
  print("Number is even")
} 
else {
  print("Number is odd")
}
> grain iffy.gr
File "iffy.gr", line 3, character 0-line 6, character 1:
Error: Syntax error

I fixed the syntax error by moving the beginning of the else statement to the same line as the end of the if block

// iffy.gr
let number = 21

if (number % 2 == 0) {
  print("Number is even")
} else {
  print("Number is odd")
}

An additional example

// Syntax error
if (number % 2 == 0) 
{
  print("Number is even") 
} 
else 
{
  print("Number is odd")
}
// Not a syntax error
if (number % 2 == 0) 
{
  print("Number is even") 
} else 
{
  print("Number is odd")
}
@ospencer
Copy link
Member

Hey @malcolmrjones, sorry I missed your issue! That is indeed a bug; thanks for reporting.

Also—I regret to inform you that the Grain formatter will most definitely format else clauses to appear on the same line 😂 but your muscle memory here shouldn't get in the way.

P.S. else isn't required in Grain, and you made me realize that our docs are way out of date! So extra thanks for the issue 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants