Hey everyone!
This is a multi-layered question and I’ll try my best to try and phrase it cleanly.
As an abstract use case I want to have a function that is adjustable in its behaviour by a caller by passing in an optional function (think Map.merge/3
). This function is used to determine what my function does - but it’s not all, it is wrapped and calls a Protocol function on a specific return value. This protocol function might want to call the previous method, hence it requires me to pass along the initially user supplied function… I’d much prefer to be able to create an anonymous function that already is the “wrapped” version of the user supplied function but can’t seem to do it because of recursion…
in code:
# in the module MyModule
def original_method(argument, user_function) do
val = user_function.(argument)
case val do
@continue_symbol ->
MyProtocol.continue(argument, user_function)
_anything ->
val
end
end
# MyProtocol
def continue(argument, fun) do
resolver = fn(argument) -> original_method(argument, fun) end
Stdlib.function(argument, resolver)
end
I’d love the whole of original_method
to be an anonymous function by itself (built from my code + the user supplied function), but it seems impossible as it would have to pass itself along to the function it calls on the protocol.
It just feels strange overall. I can’t have it as a nice already wrapped anonymous function and even in the protocol I seem to always have to build my own resolver function calling back to original_method
.
Does anyone know of a better way to do this? The concrete code can be found here and the two methods are basically DeepMerge.Integration.do_deep_merge
(still working on naming…) and the implementations for DeepMerge.Resolver.resolve
. The use case is basically that normally the protocols for each data type know how to resolve the deep_merge, but the user has a shot at changing that behaviour should she choose to (e.g. don’t merge lists).
Thanks a lot for reading this far and helping out, any feedback appreciated !
Tobi