lua-users home
lua-l archive

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


> On Wed, Nov 20, 2013 at 3:49 PM, Pierre Chapuis <catwell@archlinux.us>
> wrote:

> It actually _is_ very straightforward code, just with different
> syntax. There's a lot of sugar going on here!  And all this extra
> syntax and rules-for-sugar means, yes, it can blow up.

Maybe we don't have the same definition of straightforward :)

This is a Lua equivalent:

    local auto_table = function(fn)
      return setmetatable({},{
        __index = function(self, name)
          local result = fn()
          getmetatable(self).__index = result
          return result[name]
        end
      })
    end

So: it is a function that takes a closure (fn). It returns an empty table
with a metatable with the __index metamethod set. When the table is
indexed, the metamethod is called.

What's weird is that when called, the __index metamethod calls the closure
and replaces *itself* with the result of that call! Too much meta magic
for me :)

-- 
Pierre Chapuis