Posted 19 October 2015 - 05:34 PM
I were experimenting a bit with metatables and got this idea:
It basically allows you to enter raw binary numbers and they will be "translated" into normal numbers which could be used for math and that stuff. It doesn't have any particular use becuase b100 would be equal to 8 so I could just enter 8 since the beginning.
So, now… Is there any better way to achieve this? maybe something less hacky?
local meta = {}
function meta.__index(_,k)
if(k:sub(1,1) == "b" and tonumber(k:sub(2,2))) then
if(not tonumber(k:sub(2,#k),2)) then error("Invalid binary sequence.",0) end
return tonumber(k:sub(2,#k),2)
end
end
function meta.__newindex(t,k,v)
if(k:sub(1,1) == "b" and tonumber(k:sub(2,2))) then
error("You can't use this name!",0)
end
rawset(t,k,v)
end
_G = setmetatable(_G,meta)
It basically allows you to enter raw binary numbers and they will be "translated" into normal numbers which could be used for math and that stuff. It doesn't have any particular use becuase b100 would be equal to 8 so I could just enter 8 since the beginning.
So, now… Is there any better way to achieve this? maybe something less hacky?