You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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")
}
The text was updated successfully, but these errors were encountered:
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 🙂
I was trying out Grain and stumbled upon some strange behavior regarding the placement of the
else
statement withif
statements. It seems that Grain requires the beginning of anelse
statement to appear immediately after the end of anif
block. I know some various coding styles place the beginning of theelse
statement on a new line. My personal preference is that else statements begin on a new line. I understand that anif
statement in Grain requires anelse
statement but I'm not sure how theelse
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:
I fixed the syntax error by moving the beginning of the
else
statement to the same line as the end of theif
blockAn additional example
The text was updated successfully, but these errors were encountered: