budgie

budgie

I’m trying to unquote multiple blocks of code dynamically depending on the include option passed when use is called. When I try to map over the handlers, and pass an anonymous function, I’m getting the error that fun doesn’t exist when unquote(fun)() is called. Not sure what I’m doing wrong as this is the first time I’m making use of this kind of macro voodoo.

defmodule AppWeb.EventHandlers do
  @handlers [timers: :handle_global_update]

  defmacro __using__([include: :all] = _opts) do
    included_handlers = @handlers

    quote do
      Enum.map(unquote(included_handlers), fn {_key, fun} ->
        unquote(fun)()
      end)
    end
  end

  defp handle_global_update() do
    quote do
    ....
    end
  end
end

Showing Posts 1 to 3

kip

kip

ex_cldr Core Team

I think you’re confusing compile-time and runtime code. For example:

quote do
  Enum.map(unquote(included_handlers), fn {_key, fun} ->
    unquote(fun)()
  end)
end

The quote block is runtime code, so fun isn’t known at compile time, only runtime. And therefore shouldn’t be unquoted.

I think you probably meant to have the enumeration occur at compile time. Something like:

for {_key, fun} <- included_handlers do
  quote do
    unquote(fun)()
  end
end
budgie

budgie OP

Thank you! Now that I’ve unquoted the fun value like that, I’m getting undefined function handle_global_update/0 for all Live Views which have it included. Seems like this is happening because it’s trying to compile the call to handle_global_update everywhere it’s used instead of taking the quoted expression before compilation.

LostKobrakai

LostKobrakai

handle_global_updates is defined in your EventHandlers module, not the modules using it. You need to either import the function or define the call fully qualified as EventHandlers.handle_global_updates().

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews