2
votes

I'm reviewing some toy examples from Lua and I found the following one over there with respect to environments:

M = {}         -- the module
complex = {} -- global complex numbers registry
mt = {} --metatable for complex numbers
   function new (r, i)
       local cp = {}
        cp = {r=r, i=i}
        return setmetatable(cp,mt)
      end
      M.new = new        -- add 'new' to the module
    function M.op (...)
    --Why does not it work?
    local _ENV = complex
        return ...
      end  
      function M.add (c1, c2)
        return new(c1.r + c2.r, c1.i + c2.i)
      end
      function M.tostring (c)
        return string.format("(%g,%g)", c.r, c.i) --to avoid +-
      end
    mt.__tostring = M.tostring
    mt.__add = M.add
complex.a = M.new(4,3)
complex.b = N.new(6,2)
--nil
M.op(a+b)
--It works
M,op(complex.a+complex.b)

The use of _ENV has no effect. However, if I use complex = _G, both lines work. How do set a local environment for M.op. I'm not asking for specific libraries, I just want to know why it does not work and how to fix it.

1

1 Answers

0
votes

M.op(a+b)

This line doesn't do what you expect, because it uses values of a and b that are available when this method is called. It doesn't matter that you set _ENV value inside the method, as by the time the control gets there, the values referenced by a and b have already been retrieved and since both values are nil in your code, you probably get "attempt to perform arithmetic on global..." error.

how to fix it.

I'm not sure what exactly you want to fix, as you already reference the example that works. If you assign complex.a you can't assume that a will have the same value without mapping complex table to _ENV.