axelson
Is there a guide for relying on optional dependencies in a library?
I’m working on a library and I’d like to include a check on if an optional dependency has been included. The easy part is adding {:some_dependency, "~> 1.0", optional: true} but after that it would be nice to have a (preferably official) guide. It seems like in your code you would have Code.ensure_loaded?(SomeDependencyModule) where SomeDependencyModule is a module that you know exists from :some_dependency.
However if you use Code.ensure_loaded? to optionally compile a module or function, your code will not get recompiled automatically if that dependency is later added to the top-level project, is there a way around that?
Marked As Solved
axelson
This is the best “guide” I’ve been able to find on this, which is to use Code.ensure_loaded?/1 with a module that you expect will be inside the optional dependency:
And one example of that approach being used is in Dataloader:
https://github.com/absinthe-graphql/dataloader/blob/91db9e4f3cb8e3b375c997d83e4b1f57f97e3bf7/mix.exs#L64
https://github.com/absinthe-graphql/dataloader/blob/91db9e4f3cb8e3b375c997d83e4b1f57f97e3bf7/lib/dataloader/ecto.ex#L1
Although there’s no way to know for sure what a module will be in any given dependency, but generally there is a module that corresponds to the application name so this isn’t a large problem in practice.
Also Liked
axelson
Does anyone have any ideas on this?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









