DanielXMoore / Civet

A TypeScript superset that favors more types and less typing
https://civet.dev
MIT License
1.33k stars 28 forks source link

Allow empty body in loops, if/else, do, comptime #1187

Closed edemaine closed 2 months ago

edemaine commented 2 months ago

I propose allowing for empty blocks in most settings. We already support them in functions, and case/when/pattern matching. This PR adds them to all loops, if/else, and for symmetry, do blocks and comptime.

CoffeeScript supports this in if/else expressions that have both if and else: both empty, empty if. I think this hints at why it is useful:

My guess is that CoffeeScript couldn't handle empty if blocks without else, and loop blocks, because they are somewhat ambiguous with postfix if/loops. But a little work on the "ExpressionizedStatement" code made it easy to not expressionize in this case of empty blocks.

For example, this fixes #1162: the surprising behavior was the loop becoming a postfix after a semicolon, but when it can be an infinite loop by itself, the behavior is intuitive (though not very useful). Similarly, @bbrk24 mentioned x := comptime loop being counterintuitive: it used to give an infinite array of comptime identifiers, but now it gives the more natural but not very useful behavior of adding an infinite loop to a comptime function.

bbrk24 commented 2 months ago

Funny enough, I encountered #1162 for the exact reason you mentioned about commenting out code. Frankly I don't find either interpretation of comptime loop "useful", but the new way is more intuitive.