4
votes

I have an Elixir module that imports some functions. I would like to monkeypatch one of those functions with my own. Is that possible? How would I do this?

UPDATE WITH EXAMPLE

The specific example I have is that for task Mix.Tasks.Deps.Compile, I would like to add some functionality to the 'compile' function.

https://github.com/elixir-lang/elixir/blob/master/lib/mix/lib/mix/tasks/deps.compile.ex

The Mix.Tasks.Deps.Compile module is fairly deeply entrenched into the Mix framework. I would like to make the minimum change, while adding in the extra functionality I want. The functionality I want to add is another condition to the cond do block.

1
This begs the question - why would you want to do this? Any specific example? Might be there's a neater way to do it on BEAM.cdegroot
@cdegroot, please see the added exampleJosh Petitt
Please post any examples in the question instead of linking to themPaweł Obrok
For this particular case, the recommended way of action seems to be to extend your functionality in your own module (you can go wild, of course, with metaprogramming here to splice in the single condition ;-)) and then use Mix' aliasing system to alias "compile" to your compile. See elixir-lang.org/docs/stable/mix/Mix.html, the "Aliases" heading.cdegroot

1 Answers

10
votes

The simple answer is: you can't. There's no notion of monkey patching on the BEAM.

The longer answer is that modules in BEAM are lazily loaded, so you could replace a module with your own implementation of it (but whole module, not just a single function). But I'm really not sure that's the right way to go for it.