lua-users home
lua-l archive

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



On 27-Sep-06, at 4:01 PM, Glenn Maynard wrote:

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.

yes it is. just set the __pow metamethod.

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.

I agree. Then we could get rid of all the operators.

function quad(a, b, c)
  return b:neg():plus((b:squared()
                       :minus((4):times(a):times(c)))
                       :sqrt())
          :div(2):div(a)
end

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.

It varies from problem domain to problem domain. If you're doing
calendric computations, for example, and you'll definitely
want % and //.

The question of "what belongs in a library" doesn't have absolute
answers, I don't think. How much does adding % bloat the language?
Maybe 200 bytes. Is it worth the inconvenience of a library call to
save 200 bytes? That's in the eye of the beholder, I would say. All
of those 200 bytes do add up.

I was pretty happy to have %, although I don't use it in every
program I write. In fact, I use it in very few programs, but the
ones I use it in a use it a lot: and most of those I would like
to have // as well.