[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lightweight function syntax: no need to change Lua
- From: Dirk Laurie <dpl@...>
- Date: Mon, 29 Nov 2010 10:36:00 +0200
On Sun, Nov 28, 2010 at 10:35:31AM +0200, Dirk Laurie wrote:
> The following syntax:
>
> ('a,b,c')('a+b+c') == function(a,b,c) return a+b+c end
>
> is available after doing this chunk:
>
> getmetatable("").__call =
> function(arg,res)
> if #arg>0 then return load(arg.."=...; return "..res)
> else return load("return "..res)
> end
> end
>
Someone said, giving no example, that this will not work
for all closures. I've found one reason: the function
messes up global variables with the same names.
Improvement (tested under the current Lua 5.2 alpha):
--[[ In a package file ]]
_mapsto =
function (arg,res)
if #arg>0 then return load("local "..arg.."=...; return "..res)
else return load(res)
end
end
--[[ In the user's program ]]
getmetatable("").__call = _mapsto
It allows monstrosities like:
> return ('x,y')('x+y,x-y')(5,2)
7 3
Do I really want people to perpetrate such things and still claim
they're writing Lua? Food for thought.
Dirk