tfwright
I have a library (MyLib) that I use for part of another app’s functionality (MyApp). I’d like to add some conditional code (an Ecto schema definition) to one of the library’s modules only if MyApp has a particular config that points to one of MyApp’s modules (also an Ecto schema). I was hoping to call a function on the MyApp module to get the info about it I need, but it appears to break compilation in certain circumstances because MyApp hasn’t been compiled yet (I was trying to call __schema__/2 to get info about an association).
One option would be to require the MyApp config specify all the data MyLib needs, but that feels a bit clunky and redundant. Is there a way to force MyApp to compile first so that MyLib can access its module’s functions during compilation time? Or is there another, better way to go about what I’m trying to do?
Appreciate any tips/suggestions.
Trending in Questions
Other Trending Topics
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
- #hex
- #security
- #metaprogramming











Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
Nothing wrong with runtime configuration. But that doesn’t help you if you’re doing meta programming at compile time.
Think of Ecto as a good example. Although all the
Repocode lives inEcto, you are required to create your ownMyApp.Repomodule anduse Ecto.Repoin it with the appropriate configuration. Then the consumer of your code calls the API inMyApp.Repo, notEcto.Repo. Thats the pattern to follow for your use case I think.Happy to provide a few pointers on implementing this approach if you get stuck - here or just DM me.
Also Liked
kip
You may need to use
Code.ensure_compiled/1to force compilation order if it isn’t being detected the way you want.The documentation notes that this is not a function you should commonly reach for. However forcing compilation order at compile time is one reasonable use.
kip
It would be helpful to know:
Application.get_env(:my_lib, :some_config)iskip
Ah, I see. Compilation order is dependencies first, then the current project. Where current project is the one that has
mix.exsin it when you didmix.compile. Dependencies are compiled first and they do not create a transitive association with the “current” project. Understandably so - libraries aren’t built to make assumptions about the projects that use them.TLDR; A dependency depending on the consumer of the dependency isn’t going to work
Last Post!
tfwright
Thank you!