Qqwy

Qqwy

TypeCheck Core Team

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

The capture operator can be used in two ways: &Foo.bar/1 (‘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?

Marked As Solved

shanesveller

shanesveller

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

Where Next?

Popular in Questions Top

New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement