|
Post by warspyking on Dec 20, 2015 20:16:42 GMT
Number metatables are extremely limited.
|
|
|
Post by Tag365 on Dec 21, 2015 13:42:15 GMT
What are the number metatables limited in and what do you think could be improved or you would add if you had anything to do with developing Lua?
There are a decent amount of metamethods as seen in the list. The main metamethods include __index, __newindex, __tostring, __concat, and __len. Some math related metamethods included are __add, __sub, __mul, __div, __pow, and __unm. The comparison methods are __eq, __le, and __lt.
|
|
|
Post by warspyking on Dec 21, 2015 14:06:40 GMT
What are the number metatables limited in and what do you think could be improved or you would add if you had anything to do with developing Lua? There are a decent amount of metamethods as seen in the list. The main metamethods include __index, __newindex, __tostring, __concat, and __len. Some math related metamethods included are __add, __sub, __mul, __div, __pow, and __unm. The comparison methods are __eq, __le, and __lt. Yes but when I give a metatable to the number datatype I can't override arithmetic operators, logic operators, heck, not even concat.
|
|
|
Post by lars on Dec 22, 2015 5:29:27 GMT
Does it work when you convert your number to a string, add metatable for string, and conver later back to number?
|
|
|
Post by warspyking on Dec 22, 2015 10:46:49 GMT
Does it work when you convert your number to a string, add metatable for string, and conver later back to number? That's a lot of effort for little result
|
|
|
Post by chimmihc on Jan 3, 2016 22:07:00 GMT
So make custom numbers XD
do
local t = type
type = function(v)
local T = t(v)
if T == "table" then
return v.type or "table"
else
return T
end
end
end
numberCache = {}
numberMT = {
__eq = function(l,r)
return l.n == r.n
end,
__le = function(l,r)
return l.n <= r.n
end,
__lt = function(l,r)
return l.n < r.n
end,
__add = function(l,r)
return number(l.n + r.n)
end,
__sub = function(l,r)
return number(l.n - r.n)
end,
__div = function(l,r)
return number(l.n/r.n)
end,
__mul = function(l,r)
return number(l.n*r.n)
end,
__pow = function(l,r)
return number(l.n^r.n)
end,
__mod = function(l,r)
return number(l.n%r.n)
end,
__tostring = function(self)
return tostring(self.n)
end,
__concat = function(l,r)
return tostring(l.n .. r.n)
end,
__len = function(self)
-- stoof
end,
__unm = function(self)
return number(-self.n)
end,
__index = function(self,i)
-- stoof
end,
__newindex = function(self,i,v)
-- stoof
end,
__metatable = "",
__call = function(self,...)
-- stoof
end
}
number = function(n)
if numberCache[n] then
return numberCache[n]
else
local new = setmetatable({n = n,type = "number"},numberMT)
numberCache[n] = new
return new
end
end
|
|
|
Post by warspyking on Jan 4, 2016 2:28:52 GMT
And a wild chimmihc appears!
I'll just reiterate, tons of work for little result.
|
|