How to create a bunch of named function captures at compile time?

The capture operator can be used in two ways: & (‘named’) and &Foo.baz(&1, 42) (‘anonymous’).

I have encountered a couple of situations where someone provides a module-function-arity tuple, but the code needs access to the function as capture.

And since this is happening at compile time, it is not possible to use the anonymous case. (And longhand anonymous functions are also not possible.) If you try, the Elixir compiler will complain that only captures with the named format are serializable.

So I am attempting to fill in this format to store this at compile-time. Filling in & is however a bit of a hassle.
The nicest form I have come up with so far, is the following, rather cumbersome, approach:

{fun, []} = Code.eval_quoted(quote do &unquote(module_name).unquote(function_name)/unquote(arity) end)

Is there a less cumbersome way?

Having done this with Absinthe, prepare for a lot of pain. Some of it has to do with whether you want to allow it to capture outside variables IE:

a = 42
&Foo.baz(&1, a)

AFAIK the above is simply doomed, and if I were you I’d ask people to supply {Foo, :baz, [42]}. In Absinthe we didn’t allow captures, but we did allow inline fn a, b, c -> end so we handled this by extracting the anonymous functions into a named function that was named after the field the function was for. Then when building a schema we have an “inlining” pass where we go through each resolver function and if it’s in a form that can be serialized we serialize it inline, and if it can’t be serialized then we just store a reference to the named function for runtime lookup.

Long story short, if you’re still designing this API, my recommendation would be to avoid doing this. It’s an arduous road.

1 Like

Try Function.capture/3: