lua-users home
lua-l archive

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


I am perhaps unique in this situation, but I'll share my opinion anyway:

Personally, I do not use most of the metamethods. In fact, I strongly
recommend against them to users of my application, and authors of
third party extensions for my application. The only two I ever use are
__gc for userdata, and __index in the simple example of foo.__index =
foo and setmetatable({}, foo). I find everything else to not only be
unnecessary, but sometimes even prohibitive.

For example, I was once using a module where every time I set foo.bar
= quux, it was actually using __newindex, claiming that this was
cleaner and more expressive. But this tripped me up and caused
difficulties in writing my configuration program. I don't remember
what it was, but I remember it was very frustrating. I've had similar
frustrations when using Python and (especially) Ruby libraries, and
even some Clojure ones, where metaprogramming facilities where used
"as a convenience to the user" and I found myself simply trying to
work around them and wishing that the author just used plain old
non-magic functions and methods.

This is part of why I'm very strongly of the opinion that
metaprogramming of any kind should be used sparingly. I mention it
because I've seen metamethods discussed heavily in the context of
implicit coercions. But based on my rule of thumb of keeping things
simple, predictable, and straightforward, I don't find that any
potential convenience this gives the end-user outweighs any
frustration it will eventually cause them.

On Fri, Aug 1, 2014 at 4:35 PM, Andrew Starks <andrew.starks@trms.com> wrote:
> On Fri, Aug 1, 2014 at 3:52 PM, William Ahern
> <william@25thandclement.com> wrote:
>> I feel like the obsession with stricter typing is turning into an
>> anti-pattern.
>
>
> This is a good point, and i think the obsession has legitimate roots.
> Lua's type system is nicely defined, unevenly used (although more
> evenly with 5.3) and not quite robust enough to do some things that
> you may want.
>
> For example, if you really want duck-typing, it's not easy to do,
> because not everything is cool with a string / number like table.
>
> The dynamic programming rule that says that you shouldn't check for
> type doesn't apply as much as it otherwise would. And so 'type' is a
> bit weird in Lua.
>
> --Andrew
>