lua-users home
lua-l archive

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


On Fri, Jan 31, 2014 at 7:34 PM, Rena <hyperhacker@gmail.com> wrote:
> Functions also have metatables, which can be interesting:

I think Andrew is right - everyone who dives deeply enough in Lua goes
through a phase of fascination with overriding global metatables.
E.g. this function composition (plus using ^ for partial application)
appeared in an early version of Microlight.

In Penlight there's an option to overload behaviour of functions so
they have iterator methods:

http://stevedonovan.github.io/Penlight/api/topics/07-functional.md.html#Sequences

> seq.import()
> ('one two three'):gfind('%a+'):printall(',')
 one,two,three,

(_Sequence_ of course is a word which now has a defined meaning in Lua
(basically, arrays) so pl.seq needs a terminological update.)

TFM observes that such tricks tend to lead to rather confusing errors:

"But there is a price to pay for this convenience. Every function is
affected, so that any function can be used, appropriate or not:

 > math.sin:printall()
 ..seq.lua:287: bad argument #1 to '(for generator)' (number expected, got nil)
"