lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Sometimes, I would like to be able to define macros in Lua ? Why ?
extend the syntax. I like some languages, expecially the lisp languages
because of that. The way you call functions is exactly the same way you
call macros. It's just a matter of evaluation.

In Lua, there is no way to extand syntax, except the generic for loop.
And I thought it could be possible to have a generic do block. I know
there is a patch that replaces the notation
	do ... end
with
	function() ... end
but it is not enough for me. here is my proposition :


[local] in [ var_a1, ..., var_an = ] var_b1, var_b2, ..., var_bn do
	inst_1
	...
	inst_n
end

would expand to

[local var_a1, ..., var_an = ] var_b1(function()
	inst_1
	...
	inst_n
end, var_b1, var_b2, ..., var_bn)


If possible, the vararg expression ... in the instruction block (before
do and end) would not match the parameters of the anonymous function
created, but would be upvalues to the block where there is the in, do,
end instruction.

What do you think ?

-----------------------------------------

Last think (about varargs). Sometimes, I need to pass those variadic
arguments as upvalues to closures, but I am forced to create a table
like { n = select('#', ...), ... } in order to let my closure use
those varargs.
Maybe it could be possible to define ... even outside variadic
functions. It would just expand the vararg of the enclosing function.
Why not ?
Actually, it is not possible. The error I get :
	cannot use '...' outside a vararg function near '...'

As an example of what I wish :

	function main(...)
		return function f()
			print(...)
			-- except the print parameters of main function
		end
	end
	f = main(1, 2, 3)
	print(f, f())     --> function: 0x906a388     1       2       3

Mildred

-- 
Mildred       <xmpp:mildred@jabber.fr> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]