Re: hex numerical literals in 5.1
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: hex numerical literals in 5.1
- From: Roberto Ierusalimschy <roberto@...>
- Date: 2005年12月22日 15:19:55 -0200
Another option:
O = {}
setmetatable(O, {__index = function (t, k)
k = string.match(k, "^x(.*)")
local v = tonumber(k, 16)
t[k] = v
return v
end})
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