lua-users home
lua-l archive

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



It depends on what you intended to achieve with monkey-patching. Did you really mean to change the behavior of all code using a certain function internally (even those whose implementation you don't know) or do you want to enhance an existing module/function to make your own programming more expressive. If it's the latter (which I assumed), the code that has already been written and tested with the old behavior in mind will not benefit. But it _can_ break (popular example: someone enhances `type` to return class names for objects, and some other module uses the result of `type` in a lookup table)! If on the other hand you want the first, to patch some error without fixing the source code, you can still do that, but it requires the `debug` module (but that's ok, because you are _actually_ debugging ;-) ). Also on the plus side: you can replace some function _for certain modules only_. (If you mean serious business, you could also monkey-patch before the module in question captures its env, e.g. first thing in the main script.)

I think we both agree that monkeypatching isn't infinitely precise. Say the user wants to modify the way *half* of the functions in the module use `sum`. Unless the module is prepared for it, the only solution is re-writing half the module functions (with or without monkeypatching) - Lua is not Lisp.

If I understood you correctly, your opinion is: since it's difficult to use properly, you want "every change to have the least impact possible" - if you want to make lots of changes, you'll need lots of monkeypatches. I can understand that.

The alternative is "turning it to the max": one change in one place changes everything that could possibly depend on it. And if the user doesn't want that, he'll have to make smaller, different monkeypatches (rewrite half of the functions in the module).

With both alternatives there are cases where a single monkeypatch will not be enough, because of that lack of precision.

So instead of deciding, I looked at how the language does things. As you say, you can monkeypatch `type` if you want, and you'll face the consequences.
There are places where Lua uses `rawget` to do certain things, but I think those places are the minority, and they look more like implementation constraints than anything else (avoiding infinite loops, things like that).

I am not saying that this is the option I "prefer". It is the option I see "implicit" in Lua though. It could be that I'm seeing what I want to see. I sure am trying hard not to.

I always meant to try Löve, but I've never got around. It uses zip files for storing modules, right?

Yes, but it also works expanded in a folder. Try it, it's fun.

 `my/module/init.lua`. If it is require'd as `require "my.module"`

You are right. I will have to make a special rule for init.lua. That's unfortunate. Thanks for bringing it to my attention!
 

Enrique (kikito / @otikik)