[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why is "then" required in the if statement?
- From: Rici Lake <lua@...>
- Date: Sat, 1 Dec 2007 17:02:50 -0500
On 1-Dec-07, at 4:11 PM, Fabien wrote:
You can get rid of the "then" without adding ambiguity for the
compiler. Ditto for the "do" in "while/do/end" and "for/do/end". There
is a patch somewhere allowing this.
Yes but it is not always unambiguous. Consider:
if x or y (x or y)() end
The only way to write that without 'then' would be to use a semicolon,
but that's
currently ungrammatical (there are no empty statements in Lua) and in
any event not
particularly readable.
The case of making 'do' optional is even worse. Consider:
local result = {}
for k, v in pairs(t) -- do --
do local x = make_a_huge_object(v)
result[#result + 1] = summary(x)
end
do_a_long_computation(k)
end
The first do (the one commented out) must be either required or
prohibited. If it
is optional, the above loop cannot be parsed correctly with a limited
lookahead parser.
If it is prohibited, you'll end up with the same ambiguity as in the if
statement above.
But if you want to save a couple of keystrokes, don't suppress
keyword; pick a proper editor and customize its macros. The main
interest of a well designed syntax is to improve code readability, and
thus maintainability. It's always at least partially a matter of
taste, but I'd guess most people would find "if foo then bar end" more
readable than "if foo bar end". I definitely would, at least.
Me too. You might even just customize your editor's tab-completion, if
it has such a feature.