title: nesting metatables in lua
body: <p>In the code below it's my attempt to nest metatables on __index, but it's not working. What I want to do is if the value is t1 or t2 then return the associated value, otherwise call the function on the innermost __index. Is this possible? </p>
  
  <p>so in the below x["hello"] can I return a value. I know I can just use a function on the outermost __index, but it seems I should be able to do this somehow using nested metatables. </p>
  
  <p>tia.</p>
  
  <pre><code>x = { val = 3 }      -- our object
  
  mt = {
      __index = {
          t1 = 2,
          t2 = 3,
          __index = function (table, key)
              print("here"..key)
              return table.val
          end
      }
      }
  
  setmetatable(x, mt)
  print(x["t1"]) -- prints 2
  print(x.t2) -- prints 3
  print(x["hello"]) -- prints nil???
  </code></pre>
  
  <p>This works but it seems like I could do it with metatables</p>
  
  <pre><code>x = { val = 3 }      -- our object
  
  mt1 = {
          t1 = 2,
          t2 = 3
      }
  
  mt = {
          __index = function (table, key)
              local a = mt1[key]
              if a == nil then
                  print("here"..key)
                  a = "test"
              end
              return a
          end
  }
  
  setmetatable(x, mt)
  print(x["t1"])
  print(x.t2)
  print(x["hello"])
  </code></pre>