case statement with a single when clause fails
-
Noticed that if I try to use a case statement, I'll have to have multiple when clauses, otherwise I'll get syntax error.
(I tried to include a screenshot, but got parse error from the forum...)
The following works:
case when condition == true: "This is true" when condition == false: "Now it is false" else "Something else" end
But this will end up with syntax error:
case when condition == true: "This is true" else "Something else" end
Using build 23242 on Docker
-
I'll look into the issue, but two things:
- A
case
with only a singlewhen
(and possibly anelse
) is anif
statement, so why not use that? This is the workaround/recommendation. Usingcase
in this way just seems unnecessarily complex
. - Saying
x == true
is redundant. I see this a lot, and my old-school software guy brain just chirps a little every time. If you knowx
is boolean, you can just writeif x then
orwhen x then:
. What you're doing is comparing true for equality to true and getting true as a result, or comparing false to true and getting back false -- you're getting out of the expression the value that you already have. I come from an era where CPU cycles and RAM were very precious commodities even in the largest systems, and my brain is still wired to remove wasted cycles like this.
- A
-
A case with only a single when (and possibly an else) is an if statement, so why not use that? This is the workaround/recommendation. Using case in this way just seems unnecessarily complex
Well that's true and I don't think anyone uses the case-when structure with a single when. But for example in my case, I was testing the expressions, had written only one section and was getting this mysterious syntax error. As a software engineer myself, coming from C / Java / JS background, I naturally associated the case-when structure to switch-case where it's perfectly fine to have a single case, although as you mentioned, does not make much sense.
This is not a bug that needs to be necessarily fixed ASAP, but it would be nice if you could add a clarification to the manual, stating that the case-when structure must have at least two when sections.
-
It'll be fixed in the next release. You are right; it should work. The bug actually arises from the
else
, not the singlewhen
. Oopsie! -
T toggledbits locked this topic on