Begin End Proposal |
|
This is a proposal for the addition of 'begin/end' syntax for anonymous, zero-argument functions, aka "procs". For example, this:
begin print "Foo!" end
would be effectively transformed into:
function () print "Foo!" end
This could be added in LuaFiveTwo, or possibly in Six (though admittedly Six is a fairly faraway time frame for a relatively minor feature like this.)
The precise modifications to the syntax could be expressed as:
proc ::= 'begin' block 'end' exp ::= nil | false | true | Number | String | `...´ | function | proc | prefixexp | tableconstructor | exp binop exp | unop exp args ::= '(' [explist] ')' | tableconstructor | String | proc | function
(This adds to the grammar expressed in the Lua 5.1 reference manual.) The 'begin' keyword would be added to the list of keywords, and as such would become unavailable as an identifier.
When a 'proc' is encountered, a function is created with zero arguments and the block as the body. Any arguments passed to said function are passed as variable arguments, accessible by the ... expression (and the arg
table if that functionality remains when this proposal is implemented).
The other notable modification to the syntax is the ability to pass either a proc or an anonymous function straight to the function without parens, like is currently done with table constructors and the like.
The current syntax which could generate something like a proc is verbose and unwieldy.
function () do_stuff() end
The new syntax would be much easier to read, easier to type, and easier on people using Lua as a configuration language or DSL. The ability to pass functions and procs to function calls without parens would serve a similar purpose. It would require minimal modifications to the Lua core, and code using procs would treat them no differently than normal functions.
This is an example I thought of. (Of course, there are many other places this could be used.)
doc = html begin -- 'html' is a function in the local namespace head begin -- 'head' is automatically generated by the environment title "Hello, world!" end body begin h1 "Hello, world!" end end
At the bytecode level, this would be completely backwards compatible - procs could generate the same bytecode as the function () ... end
syntax.
Syntactically, the changes would be minimal. The only syntax added that could cause problems with existing code is the addition of begin
as a keyword. (The other syntax additions were not even remotely possible before, and should not cause issues with current code.) However, begin
isn't a very good identifier - there is precedent in other languages for its use as a keyword, and often start
is a better fit in situations where begin
could be used.
I originally thought of the use of start
as a keyword. However, there are major problems with this. First, it is used as an identifier much more often than begin
. Second, start
has never been used in this setting to my knowledge, but begin
has in many languages. Finally, begin
sounds better and takes the same number of characters to type.
Another way to approach this would be to use a delimiter. Using curly braces is not practical, as they are already used by tables - and it would be much more difficult to distinguish tables from code blocks in this case. Square brackets have similar problems, in that it would be too easy to have them confused with long-string delimiters. And angle brackets have never been used this way before, and could be misconstrued as a binary comparison operator. Parens are just ridiculous. And using words is more of the Lua way.
This proposal was written by LeafStorm.
Lua code using syntax introduced in this proposal is written in normal monospace format to avoid confusing the syntax highlighter.
This is the end of the actual proposal.