I’m dealing with a project, where a function A from module B is called from the two following places:
a “regular” function - which is called from within a Phoenix controller.
an anonymous expression which is passed to the following macro.
defmacro other_function(anonymous_function) do
quote do
MyRepo.transaction(unquote(anonymous_function), timeout: 60000)
end
end
When 2) is called, an UndefinedFunctionError occurs, because of the call to function A.
I understand that unquoted bit of macros aren’t evaluated/checked at compile time, thus why there is n compilation problem. However, since the other non place calling A has no problem, I can’t understand why it only occurs in the Macro, during the transaction. The place where anonymous function is defined has full access to module B, so I’m guessing I’m not understanding how to use macros/transactions in some way.
do you really need a macro for what you’re trying to do? why do you need to mess up with the AST?
I’m assuming that the macro you provided is a simplification of what you need, but if it’s just as you showed, you’re good with a plain function doing that.
Currently :bind_quoted isn’t being used. As I understand, that is for extracting the value of variables passed to the macro, for when you do something with them. In this case, the macro seems to only exist to wrap the transaction function with the given timeout. I think I need to sleep on it and have another go at that documentation, because I’ve read it so many times and am still not getting it.
full disclosure, I didn’t write this codebase, and am having trouble getting a response from the original author -so I can’t really speak for the intentions of it. As far as I can tell, it exists to simplify explicitly specifying the transaction timeout, and not a lot else.
The suggestions are highly appreciated!
Turns out the problem was, the app where the function was in a different one to where the function was defined - both of the apps run and compile under the same umbrella.
I added the function’s app to the dependencies defined in the mix.exs file for the other app, and the problem is solved