顯示具有 table 標籤的文章。 顯示所有文章
顯示具有 table 標籤的文章。 顯示所有文章

2014年10月13日 星期一

LUA read only table

-----------------------------------------------
-- file DIR.lua

function readOnly (table)
  local proxy = {}
  local mt = {       -- create metatable
    __index = table
    ,__newindex = function (x,k,v)
      error("read-only "..k, 2)
    end
    ,__metatable = false
  }
  setmetatable(proxy, mt)
  return proxy
end

local t = { LEFT = 7, RIGHT = 8 }
DIR = readOnly(t)

---------------------------------------------
-- file test.lua
dofile("DIR.lua")

print("t="..tostring(t))

print("LEFT="..DIR.LEFT)
-- DIR.LEFT = 9                -- : read-only LEFT
print("LEFT="..DIR.LEFT)

mt = getmetatable(DIR)
print("mt="..tostring(mt))

---------------------------------------------
-- output
t=nil
LEFT=7
LEFT=7
mt=false

食用油

豆大芥小 加熱均 又非橄㰖 造假多 油麻菜籽 螻蟻命 自求多福 身安康 目前 我推 芥花油,較耐高溫. 從成份上來看 橄㰖>芥花>大豆 . 從CP/造假上來看 又不推 高價 橄㰖了, 可能 摻其它大豆等油/加色素等, 最近新聞 數千公頓大豆 不知所踪, 合理推測, 是...