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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

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 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement