[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Scheme's "delay" function in plain Lua?
- From: Michal Kottman <k0mpjut0r@...>
- Date: Fri, 29 Oct 2010 13:27:03 +0200
On Fri, 2010-10-29 at 18:57 +0800, Johnson Lin wrote:
> I'd hoped to use an interface similar to this:
> local c = delay(a+b) -- however this is quite not possible without
> language support
You can do stuff like this, but not with standard Lua. You need some
metaprogramming mechanisms, like provided by Metalua, where the example
looks like this:
local c = || a + b
"|x,y,z| exp" in Metalua is a shortcut for "function(x,y,z) return exp
Maybe you can achieve similar results with token filters. There has been
a lot of discussion about simple syntax for short anonymous functions on
the list, if you are interested.
> so instead I was trying:
> local c = delay("a+b") -- maybe we can do some magic on the string
> or even
> local c = delay("+", a, b) -- etc...
Yes, passing a string and compiling it is plausible - pseudocode:
local f = assert(loadstring("return function() return " .. exp .. "
-- TODO: setup the env of f to lookup local variables
The problem is that all the variables are resolved as globals. You need
to do some tricks with debug.getlocal and setfenv, so that the resulting
function resolves variables first in the locals, and only after that in