lua-users home
lua-l archive

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


Gavin Kistner wrote:

How about:

return A[k]~=nil and A[k] or B[k]~=nil and B[k] or C[k]~=nil and C[k] or error( "Not Found" .. k )
still no good... A[k] = false, B[k] = true will return B[k}, not A[k].

You can define a placeholder for false...

local FALSE = {}

then use FALSE for false, and substitute to<->from where required.

ie
set:
t[k] = (v == false) and FALSE or v
get:
return (t[k] ~= FALSE) and t[k]


join3 = function(A, B, C)
  local t = { }
  setmetatable(
    t, {
     __index = function(t, k)
         local r = A[k] or B[k] or C[k]
         return (r == nil) and error("Not found" .. k) or r ~=FALSE and r
      end;
     -- Skip __newindex for clarity. Must substitute false with FALSE
    }
  return t
end


Adrian