lua-users home
lua-l archive

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

Another option:

O = {}
setmetatable(O, {__index = function (t, k)
  k = string.match(k, "^x(.*)")
  local v = tonumber(k, 16)
  t[k] = v
  return v

print(O.x34)    --> 52

(O.x34 is "almost" 0x34 :) The memoizing is important in real programs,
where many constants appear inside functions and loops (and therefore
are evaluated multiple times).

-- Roberto