Is there any means to (temporarily) redefine a function, so that:
f = function(x) 0.5 end
# monkey patch Base.sin with f
sin(12.0) # returns 0.5
# undo monkey patching
I suppose it was once possible, done like here: https://github.com/burrowsa/Fixtures.jl/blob/master/src/mock/patch.jl#L23
Now, doing something similar is not possible anymore, since sin
has no fields like code, fptr, env
, which I assume defined the function back in 0.3.
I need such thing to temporarily override any function’s behavior to isolate tests from effects of stuff like Base.open
etc.
One vague idea I have would be to redefine a function for particular argument types i.e. import Base.sin
, redefine sin(::Float64)
, test, undo (somehow). But that seems to require much more effort - to get the types right for the patch to work as intended. And it would produce redefinition warnings. Is there any other way?