[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A guide to building Lua modules
- From: Coroutines <coroutines@...>
- Date: Mon, 14 Apr 2014 09:21:19 -0700
On Mon, Apr 14, 2014 at 9:10 AM, Aapo Talvensaari
<aapo.talvensaari@gmail.com> wrote:
> On Mon, Apr 14, 2014 at 6:29 PM, Coroutines <coroutines@gmail.com> wrote:
>>
>> Looks pretty thorough, good job! The only thing I saw that might be
>> worth mentioning is some modules monkey-patch globals
>
>
> I wish that there was a way to monkeypatch variables with monkeypatching
> scope limited.
>
> E.g. a function that takes a string as an argument, and then patches more
> functions to that (right now the only way to add more string methods is to
> patch the global string metatable, or wrap string inside something else).
> Also for patching tables with limited scope (i.e. patching a table inside a
> function would affect the table only inside the function). Maybe we need a
> function like setmonkeypatches(variable, {}) (compare that to setmetatable).
>
> I could use it for example here:
> https://github.com/bungle/lua-resty-template/blob/master/README.md#template-helpers
>
> (Adding methods to global string, and table types (not encouraged, though))
>
> But I assume this only adds unneccessary cruft to the language, and probably
> also hard to debug situations (why does this variable have a function here,
> but not here?).
You could do much of this with proxy tables (empty tables where you
have a metamethod with a defined __index/__newindex to catch changes).
It's a bit hard to grasp but here's some starter information:
http://www.lua.org/pil/13.4.4.html
It's not "easy" to disallow monkeypatching but that is how you would
go about getting that level of control. In my experience with Lua,
it's a good thing to let other people shoot themselves in the foot --
it's when they submit bug reports about issues caused by their
monkeypatching that gets on my nerves, haha