|
The following code attempts to override the __lt metamethod to allow comparison:
local function CompareAmounts(first, second)
return first:GetAmount() < second:GetAmount()
end
Value =
{
New = function(amount)
assert(type(amount) == "number", "Invalid value amount")
local self = { amount = amount }
local function GetAmount()
return self.amount
end
return
{
GetAmount = GetAmount,
__lt = CompareAmounts,
}
end,
}
math.randomseed(os.time())
first = Value.New(math.random(100))
second = Value.New(math.random(100))
if first < second then
print("The first value is smaller")
else
print("The second valud is smaller")
end
However, running the program will produce a result like the following:
lua: Value.lua:29: attempt to compare two table values
stack traceback:
Value.lua:29: in main chunk
[C]: ?
What is needed allow for this type of comparison?
|