cjbottaro
Easiest to ask this in code…
defmodule Foo do
use Tagger
tag(:red)
end
defmodule Bar do
use Tagger
tag(:blue)
end
Then at runtime I want to be able to say:
Tagger.all_tagged # %{ Foo => :red, Bar => :blue }
How to define Tagger to do this? I know how to use module attributes and macros to create methods on Foo and Bar to keep track of what they are tagged with, but how to aggregate that info into another module?
Thanks for the help.
Trending in Questions
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
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
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
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
voltone
I would probably use module attributes to store the tag in the compiled module:
You can then inspect the attributes using
module_info/1:Given a list of modules you can then simply map, filter, aggregate, etc. using Enum.
As for the list of modules, you can use
{:ok, modules} = :application.get_key(:my_app, :modules)to get all modules for your OTP app. Or, if you want to scan for modules more broadly, you can look at iex’s autocomplete for inspiration:https://github.com/elixir-lang/elixir/blob/master/lib/iex/lib/iex/autocomplete.ex#L247
cjbottaro
Oh wow. A lot more complicated than I thought, but this solves it! Thanks a ton!
cjbottaro
Just for posterity, the
:code.all_loaded()technique didn’t work for me. I’m not sure what marks a module as “loaded”, but when I started an iex console and called:code.all_loaded(), my modules didn’t show up in the list.Here’s what I did instead:
Now what marks an application as loaded? I have no idea. But
:application.loaded_applications()seems to return all applications in my mix project… at least at the point where I’m calling it (which is the mod: callback).OvermindDL1
It works for any code that is indeed loaded. It is considered loaded when some function on the module is called, until that point it is not loaded (unless the erlang compiler option in the boot forces loads everything in the one file). Your modules will appear once you call something on them.
An application is loaded by the boot loader, but again that gets back into manual plugin definitions instead of dynamic detection. ^.^
voltone
If you look closely you’ll see that the IEx code only relies on
:code.all_loaded()alone when:code.get_mode()returns anything other than:interactive.When you start your application as part of a release, the
:embeddedmode is used, and all modules included in the release a loaded right after the VM has started. On the other hand, when you runiexon your Mix project, the code server uses:interactivemode in which modules are located and loaded on-demand, as @OvermindDL1 described.As for application loading,
iex -S mixloads your main application and all its dependencies (actually, it first loads the dependencies and then the main app). Some dependencies may explicitly load/start additional applications as they initialize.lguminski
I was also thinking of using module attributes to store the tags (that’s how I found this thread), but eventually I dropped the idea. The reason was that (according to documentation) stripping debug symbols removes all module attributes:
I haven’t tested it, but if it is so, then it may happen that the code appears to work, until somebody decides to strip debug symbols (e.g. while releasing to production) and then the code mysteriously breaks. So using this approach at runtime it potentially dangerous.
cjbottaro
Somewhere over the last two years, I noticed that Elixir records behaviours in module attributes, which is actually the use case I was going for. So here’s the code I use now:
Which apparently relies on the debug info not being stripped to work…
Thanks for the tip!
Any ideas to work around the problem?
cjbottaro
Forgot to mention… that above code snippet works when using Elixir releases.
wolf4earth
Depending on how fast you want to make this you could also try to do all this work at compile time.
But that carries it’s own compilations, you obviously want all modules using
Taggerwhich means you somehow need to wait until everything is compiled.I’ve found this thread on the matter but it seems this is no simple endeavour.
lguminski
I think the best option is to use @before_compile hook for tagging. Check the example below:
And this is how you use it:
So effectively “@before_compile Tagger” does the tagging.