Wigny

Wigny

Escaping anonymous function in module attribute

I’m currently writing a specie of DSL and I’m trying to understand how can I escape an anonymous function in a module attribute. I’m not sure how it can be achieved and even tried without success to understand how Absinthe does it with the resolve macro (e.g. field :name, :string, resolve: _, _ -> ... end [Absinthe.Type.Field — absinthe v1.7.6]). This is what I have tried so far:

defmodule DSL do
  defmacro __using__(_opts) do
    quote do
      import DSL, only: :macros

      Module.register_attribute(__MODULE__, :columns, accumulate: true)

      @before_compile DSL
    end
  end

  defmacro __before_compile__(_env) do
    quote do
      def __columns__, do: Enum.reverse(@columns)
    end
  end

  defmacro column(field, resolver) do
    quote do
      @columns {unquote(field), unquote(resolver)}
    end
  end
end

defmodule User do
  use DSL

  defstruct ~w[first_name last_name email]a

  column :name, fn user -> "#{user.first_name} #{user.last_name}" end
  column :email, fn user -> user.email end

  def test do
    user = %__MODULE__{first_name: "John", last_name: "Silva"}
    columns = __MODULE__.__columns__()

    then(user, columns[:name])
  end
end

which fails with:

** (ArgumentError) cannot inject attribute @columns into function/macro because cannot escape #Function<1.54200728/1 in :elixir_compiler_0.__MODULE__/1>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity
    (elixir 1.16.0) lib/kernel.ex:3648: Kernel.do_at/5
    (elixir 1.16.0) expanding macro: Kernel.@/1
    /home/wigny/workspace/dsl.exs:25: User.__columns__/0
    /home/wigny/workspace/dsl.exs:25: DSL.__before_compile__/1

I’m aware I could simply solve it by replacing the anonymous function call with a captured function call, but I would like to understand how libraries like Absinthe solve this problem…

and Happy New Year to everybody :).

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I cannot overemphasize how much I regret allowing this in Absinthe :D. :persistent_term sort of saves my bacon but that required rewriting the whole DSL into something that also operates at runtime which is sort of worth it but now we’re into a whole OTHER discussion of runtime vs compile time DSLs.

Supporting compile time anonymous function is a small nightmare. In the compile time version of Absinthe schemas where they’re compiled to a module body the answer simply is: I don’t. The AST of the anonymous function gets “lifted” up and then dropped into the function body of a __absinthe_function__ clause so that they can be fetched at runtime as part of middleware. In the :persistent_term version I think they get inlined? Honestly I haven’t looked at the details in a while.

Here’s why they’re a nightmare. Let’s take your nice simple example:

  column :name, fn user -> "#{user.first_name} #{user.last_name}" end
  column :email, fn user -> user.email end

And add stuff that as the end user you think should work:

  @prefix :foo
  column :name, fn user -> "#{@prefix} #{user.first_name} #{user.last_name}" end
  @prefix :bar
  column :email, fn user -> "#{@prefix} #{user.email}" end

OR

  prefix =  :foo
  column :name, fn user -> "#{prefix} #{user.first_name} #{user.last_name}" end
  prefix  = :bar
  column :email, fn user -> "#{prefix} #{user.email}" end

OR

  column :name, fn user -> "#{Application.get_env(:my_app, :prefix)} #{user.first_name} #{user.last_name}" end

etc.

These get horrendously tricky to implement because module attributes haven’t actually been executed at the point that your macro is firing, they’ve only become AST itself, and other details depend wildly on how your store your function. It’s even possible to store functions that can’t be used at runtime at all!

Absinthe was built before Persistent term existed. If I could do it all over I’d have a purely functional DSL that just made a nice data structure, and then I’d require that you:

children = [
  ... # ecto goes here
  {Absinthe.Schema, your_schema_builder_result_here},
  # ... Other stuff like phoenix
]

and Absinthe would consume the data stucture emitted by the functional DSL and put whatever structures into :persistent_term it deemed efficient. For Absinthe 2.0 (no immediate plans or timeline) that is exactly what I’ll do. Even for a lot of things like resolvers though it’s just way clearer to point to a “regular” function via &Module.function/3 because there’s no closure over a complicated and often difficult to determine environment.

This is all of a very long winded way of saying: I don’t recommend this style of DSL. Strongly consider other options to avoid it. cc @zachdaniel since he’s done a bunch of similar stuff over in Ash that has at this point far surpassed what I did DSL wise in Elixir.

zachdaniel

zachdaniel

Creator of Ash

Honestly we’ve pretty much “figured it out” with our DSL builder Spark. It escapes functions just fine by defining a named function in the module and referencing that. If you have a DSL option type that is a function, this all gets handled for you automagically. You can also use a higher level type {:spark_function_behaviour, behaviour, {function_mod, arity}} that a DSL option can adopt. In that case, you can provide a behaviour and opts pair like @benwilson512 illustrates, or you can provide an anonymous function of a given arity. And it defines a function in the module and references that, passing it to {TheMod, fun: TheFun}.

Spark is still sorely undocumented, but it’s heavily battle tested and has good tests that you can learn from :slight_smile: you could build all of absinthe’s DSL or phoenix router in spark with the tools it has today. And we provide an elixir_sense plugin that provides autocomplete with in-line DSL docs with no action required at all.

Edit: link to spark Get Started With Spark — spark v1.1.53

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement