Packaging for multiple Elixir versions

I have a package published with Hex, and when I wrote it, 1.10.3 was the current release of Elixir. I know that 1.11 added some stuff, and I don’t think it impacts my code (I still need to verify this), but it made me wonder, how are we supposed to be supporting Elixir versions? I’m guessing I’d have to do some branching of my code to have a 1.10.x version and a 1.11.x version if I ever wanted to add additional features in newer releases? Then is there something within the mix tools that lets me publish multiple copies or something? I poked around the web for answers but I’m not certain what the best method is

1 Like

Then is there something within the mix tools that lets me publish multiple copies or something?

Nope.

I’m guessing I’d have to do some branching of my code to have a 1.10.x version and a 1.11.x version if I ever wanted to add additional features in newer releases?

Yup!

The most common option is to check if a given function (that is only available in the most recent release) is defined. Let’s say you want to use Enum.product/1 that will be released in Elixir v1.12.

I’d do something like this: (along with a TODO as a reminder)

def f(x) do
  enum_product(x) / length(x)
end

# TODO: remove when we depend on Elixir v1.12
if Code.ensure_loaded?(Enum) and function_exported?(Enum, :product, 1) do
  defp enum_product(x), do: Enum.product(x)
else
  defp enum_product(x), do: # custom implementation
end

Stuff like this adds code complexity and maintenance burden so it’s usually a good idea to have pretty aggressive minimum Elixir version requirement. I’d consult Compatibility and deprecations — Elixir v1.16.0 and see which version still receives bug fixes and target around that. After all, your users should be on recent Elixir versions to receive those bug fixes.

5 Likes

This is exactly what I need. Thank you!