[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0
- From: David Jones <drj@...>
- Date: Wed, 18 Jun 2003 14:41:39 +0000
In message <20030618135518.GA29228@malva.ua>, Max Ischenko writes:
> I have this code snippet:
> function compile(spec)
> local funcBody = string.format('return function() return %s end', spec)
> return loadstring(funcBody)
> f = compile('2==3')
> This prints 0 on Lua 4.0 and function: 0x804eb60 on Lua 5.0
> To print false (a.k.a 0) in Lua 5.0 I had to wrote
> Why I this? Did I write compile function wrong?
Presumably you were using dostring in Lua 4.0 and are now using
loadstring in Lua 5.0? Although the manual deprecates dostring in
favour of loadstring they do not have identical behaviour.
dostring will compile _and_ execute the string.
loadstring will compile it and return a function that when called has
the same effect as executing the string.
In Lua 4.0 this 'return function() return ... end' was a way to achieve
the delayed execution that loadstring already does in Lua 5.
in Lua 5.0 use
return loadstring(string.format('return %s', spec))
Hope that helps.