fellipessanha

fellipessanha

How to conditionally compile a module with 3rd party macros

I’ve been working on a wrapper library for a brazillian Open Finance api, and I incurred into a fun error whilst trying to conditionally compile a module with 3rd party macros. I wrote a short text about this on medium, if you’re interested in the full story.

Also, I’m not too sure if ‘conditionally compiling a module’ is the best way to call this, but it’s how I’m able to describe this issue.

The code that errored looked a little like this:

  defmodule ConditionallyRunCode do
    if Code.ensure_loaded?(Phoenix.LiveView) do
      use Phoenix.Component
    else
      raise "no module 🤷"
    end
  end

and returned this error:

    error: module Phoenix.Component is not loaded and could not be found
    │
 36 │     require Phoenix.Component
    │     ^
    │
    └─ lib/pluggy/connect/live.ex:36:5: Pluggy.Connect.Live (module)

As weird as it looks, the solution was to define modules inside the if statement, and I briefly explain the reason why in my medium text, but the solution looked like this:

  if Code.ensure_loaded?(Kino.JS.Live) do
    defmodule ConditionallyCompileCode do
      use Kino.JS.Live
    end
  else
    defmodule ConditionallyCompileCode do
      raise "no module 🤷"
    end
  end

I didn’t really like how this solution looks, so I’d like to hear from the community:

Have you ever encountered an error like mine? Is there a better way to solve this issue?

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
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

We're in Beta

About us Mission Statement