顯示具有 read only 標籤的文章。 顯示所有文章
顯示具有 read only 標籤的文章。 顯示所有文章

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

常用 氧化活性 排行

 氧化 化學反應 由高至低: 鉀鈉鋇鍶鈣鎂鋁碳鋅鉻鐵鈷鎳錫鉛氫銅汞銀鉑金