lua-users home
lua-l archive

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


On Mon, Nov 18, 2013 at 7:20 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
What would be a sensible mapping of operators to metamethods?

Functional programing hacks are fun, but I think it's not sensible encourage using them in Lua. Functional thinking mixes poorly with mutable values, and Lua tables are designed to be used through mutation rather than functionally.

Whenever you try to use tables functionally, either you make a lot of systematic copies, many of them actually unnecessary, or you end up with shared references, alter a table from one reference then causing bugs in the other reference holders, thus facing the very kind of bugs you're supposed to get rid of by adopting functional programming. Even if you "copy everything", you're confronted to the fact that there's no obvious definition of what means "table copy": shallow copy? Deep (hence arbitrarily expensive) copy? Including metatables?

A fun experiment would be to add a purely functional (non-mutable) container in Lua, and see how well it could cohabitate with imperative tables (the result would probably not feel lua-ish at all; maybe more scheme-ish). But my experience is, it rapidly becomes painful to pretend that tables can be used functionally.