lawik

lawik

Nerves Core Team

Can a library find all modules implementing a Behaviour?

I have a library that wants to find all modules for a given behaviour. I found some description that showed how to do this off of :code.all_loaded() which was neat. Then I moved the library out of the application it was living in because it really isn’t supposed to be an application on its own. And things started breaking intermittently during testing.

So that’s when I found out about Elixir doing lazy loading of modules through this thread. But the build_embedded stuff only really seems to apply to an application.

Should I just use another approach for this? I want to find all modules that are widgets. I was doing this at runtime, building the list when it was first requested and persisting it to the Application environment. I don’t really care if the initial list is made at compile time or runtime. Ideally it should also be able to register new Widgets if they are loaded at runtime or come from outside the library.

Is there a common pattern recommended here that I’m just not familiar with? I realize this is state but the library is mostly pure functions so avoiding needing to start a supervision tree for the lib would be more elegant.

Most Liked

LostKobrakai

LostKobrakai

I’ve tried to do similar things in the past and tbh the best/most flexible thing you can do it let your users provide a list somewhere. All other options have downsides like needing global state or your case of a certain embedded mode, …

christhekeele

christhekeele

I messed around with metaprogramming using module compilation hooks to achieve a way to track what modules use other modules a few years ago. It could be applied here to track behavior usage.

You have to own the module being used, and clients have to use use instead of @behaviour. But it will let the library track all modules at compile time, across other libraries and umbrella apps.

Not that this probably isn’t a great idea, one of the whole points of having behaviours is to define an interface that others can call dynamically with arbitrary modules.

Also note I haven’t tried this out for years, so may not work verbatim on recent elixirs.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
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 t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement