Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MIX dependencies have a "~>" before the semantic version of the dependency?

On mix.exs you can declare dependencies like:

def deps do
  [{:plug, "~> 1.0"}]
end

Why does it need to have "~>" instead of simply the version on the second part of the tuple.

I have seen that if it gets the dependency from git, you can write the dependency like:

def deps do
  [{:plug, git: "git://github.com/elixir-lang/plug.git"}]
end
like image 584
Camilo Sampedro Avatar asked Nov 20 '17 14:11

Camilo Sampedro


People also ask

What does mix DEPS get do?

get (Mix v1. 12.3) Gets all out of date dependencies, i.e. dependencies that are not available or have an invalid lock.

How to add a dependency in Elixir?

To add a dependency, you have to add the dependency and version to the list in the deps function. Unlike package managers like npm and yarn, you cannot add dependencies from the command line. A dependency entry looks something like this: { :dependency, "~> 1.0. 0" } .


1 Answers

This fancy arrow is supported by Version module. It rounds your dependency with precision set by the digit, which is left neighbour of the most specific one.

From the docs:

# 2.0.0 and later until 2.1.0
">= 2.0.0 and < 2.1.0"

Since the example above is such a common requirement, it can be expressed as:

"~> 2.0.0"

Check more examples in the Version module.

Basically it's for your convinience, because it allows you to upgrade your deps automatically whenever you do mix deps.upgrade, but it lets you control the upgrade - you could download the upgrade, which crashes your current codebase etc.

like image 147
PatNowak Avatar answered Oct 04 '22 17:10

PatNowak