lua-users home
lua-l archive

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


On Thu, Sep 15, 2011 at 08:36:09AM +0200, Eric Man wrote:
> 
> If you run the code snippet, it'll print "= a<a<". I'd have thought it would be "= a<<.>a<<.>>".
> 
> If you toggle line 4 off, though, it will print "= a<<.>a<<.>>" as expected.
> 
...
> local function test(bool)
>   local w, t
>   if bool
>  --or true  --- toggle this line
> then
>     t = "a"
>     w = function (s) t = t .. tostring(s) end
>     _ENV.w = w
>   else
>     t = "."
>   end
> 

Since 'w' never appears outside 'loadstring', the local 'w' is
irrelevant, and you can get rid of the '_ENV'.  (Once you do that,
Lua 5.1 also runs the snippet.)

The above code simplifies to

    local function test (bool)
      local t
      if bool then
        t = "a"
        function w(s) t = t .. tostring(s) end
      else
        t = "."
      end

Whereas the code below, since the 'or true' means the condition
always holds 

> local function test(bool)
>   local w, t
>   if bool
>  or true  --- toggle this line
> then
>     t = "a"
>     w = function (s) t = t .. tostring(s) end
>     _ENV.w = w
>   else
>     t = "."
>   end
> 

simplifies to

    local function test (bool)
      local t = "a"
      function w(s) t = t .. tostring(s) end

Dirk