Hi Michał,

Thanks for the proposal. Your initial description makes me think there may
exist bugs which we would need to investigate first.

2. Users of a library with optional dependencies have to include all
optional dependencies in their mix.exs

This should not be required. You only need to include the dependencies that
you need, which would be equivalent to opting into a feature in Rust.

3. Users might include bad varsions of optional dependencies

This should not be possible. The requirement has to match for optional
dependencies.

If the above is not true, please provide more context.

---

Other than that, you should be able to provide this functionality using
config/config.exs files and the Application.compile_env/2. In fact, I think
introducing another mechanism to configure libraries could end-up adding
more confusion, especially given how configs changed (and also improved)
throughout the years.



On Tue, Mar 7, 2023 at 12:40 PM Michal Śledź <michal.sl...@swmansion.com>
wrote:

> Currently, using optional dependencies is quite inconvenient and error
> prone:
>
> 1. A lot of modules have to use if Code.ensure_loaded statements
> introducing additional nesting
> 2. Users of a library with optional dependencies have to include all
> optional dependencies in their mix.exs
> 3. Users might include bad varsions of optional dependencies
>
> My proposal is to enhance API for optional dependencies basing on the API
> provided by Cargo in Rust.
>
> The main idea is that the user of a library with optional dependencies
> specify which "features" it is willing to have. For example, in
> membrane_rtc_engine library, which allows you to exchange audio/video using
> different multimedia protocols, we have a lot of optional dependencies
> depending on what protocol the user is willing to use. When the user wants
> to receive media via webrtc and convert it to the HLS to broadcast it to
> the broader audience it has to include all of those dependencies
>
>    # Optional deps for HLS endpoint
>    {:membrane_aac_plugin, "~> 0.13.0", optional: true},
>    {:membrane_opus_plugin, "~> 0.16.0", optional: true},
>    {:membrane_aac_fdk_plugin, "~> 0.14.0", optional: true},
>    {:membrane_generator_plugin, "~> 0.8.0", optional: true},
>    {:membrane_realtimer_plugin, "~> 0.6.0", optional: true},
>    {:membrane_audio_mix_plugin, "~> 0.12.0", optional: true},
>    {:membrane_raw_audio_format, "~> 0.10.0", optional: true},
>    {:membrane_h264_ffmpeg_plugin, "~> 0.25.2", optional: true},
>    {:membrane_audio_filler_plugin, "~> 0.1.0", optional: true},
>    {:membrane_video_compositor_plugin, "~> 0.2.1", optional: true},
>    {:membrane_http_adaptive_stream_plugin, "~> 0.11.0", optional: true},
>
> Instead of this, I would love to say to the user, hi if you want to use
> HLS just specify it in the feature list. For example:
>
> {:membrane_rtc_engine, "~> 0.10.0", features: [:hls]}
>
> It would also be nice to somehow get rid of "if Code.ensure_loaded"
> statements. I am not sure how yet but Rust do this that way
>
> // This conditionally includes a module which implements WEBP support. 
> #[cfg(feature
> = "webp")] pub mod webp;
>
> What comes to my mind is that in mix.exs we can specify "features", their
> dependencies and a list of modules. When someone asks for the feature,
> those dependencies are autmatically downloaded and listed modules are
> compiled.
>
> The final proposal is:
>
> # library side
> # mix.exs
>
> features: [
>   hls: [
>     dependencies: [],
>     modules: []
>   ]
> ]
>
> # user side
> # mix.exs
>
> {:membrane_rtc_engine, "~> 0.10.0", features: [:hls]}
>
> I would love to help in implementing those features if you decide they are
> valuable
>
> Rust reference:
> https://doc.rust-lang.org/cargo/reference/features.html#features
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elixir-lang-core+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/8385965e-799d-4cea-bcd5-151d9fee6914n%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/8385965e-799d-4cea-bcd5-151d9fee6914n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2Bgg3hm15auh%3DFE3E826V4EOby5TBp_ZCZiKWOvhzK%2BrA%40mail.gmail.com.

Reply via email to