[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Comparing numerical objects for equality (patch)
- From: Andrew <ajc@...>
- Date: Fri, 10 Jun 2022 01:24:09 +1000
sorry, too quick hitting send there!
No that doesn't work if a is a number of course.
What we're trying to do is write code that doesn't need to know whether
the operands are Lua numbers or some kind of number-like object. That's
why we want to use the '==' operator and have Lua figure out whether it
can do the operation itself or call a metamethod to do it.
On 10/06/2022 01:20, Andrew wrote:
On 10/06/2022 01:08, Luiz Henrique de Figueiredo wrote:
if a == 1 then -- metamethod __eq
You can write this instead:
if a:__eq(1) then
does that work if a is a Lua number?
Not as pretty but not terrible either.
This assumes that the methods in your library work with objects and
numbers, promoting numbers to objects as needed.