lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Sep 27, 2006 at 03:10:45PM -0500, Rici Lake wrote:
> >Lua provides its standard libraries to avoid this; add math.idiv and
> >math.imod.
> 
> Sure, but those are not cleanly overrideable if I want to implement, for
> example, bignums.

This applies to every operation.  pow() is no more cleanly overridable,
or any of the myriad of operations a bignum class would want to implement.
This isn't an argument for div and mod in particular.

That said, I thought all types could have a metatable in 5.1, and it
seemed like numbers would be set to the "math" table, but I get:

=getmetatable(2)
nil

and I can't do "(2):sin()", like I can do "('foo'):len()".  Not sure
why.  If this was possible, then it'd be cleanly overridable: "a:idiv(b)"
would work for both numbers and your bignum class, and in a way that
scales to as many overridable operations as you want.

> The bitstring library I use overrides + for 'or', '*' for 'and'
> and unary minus for 'not'. I actually use those as bitstrings, not
> integers, so that works well for me. If Lua had 'inf' and 'sup'
> operators, I would have used those instead; I think that would
> be clearer. There are advantages and disadvantages to overriding
> operators, but it is clearly part of Lua, and the ability to
> override numerical operators is presumably in order to allow the
> implementation of number-like objects, like bignums, continued
> fractions, complex numbers, or whatever. In that context, once the
> decision is made to implement %, which I have no problems with, it
> seems reasonable to me to implement the other half of that operation,
> euclidean division.

I don't understand the addition of % at all.  I use it all the time in
C, of course, but I never even noticed its absence in Lua, and its
addition to 5.1 just seems to bloat the core language with something
that belongs in a library.  (So, maybe you can at least understand my
impression of this argument: using one operator that feels out of place
in the language as a wedge for adding yet another.)

(I'm curious of what the rationale for adding % was.  Was it something
other than "performance"?)

-- 
Glenn Maynard