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

1

13

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
Notate answered 20/11, 2017 at 14:24 Comment(0)
P
18

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.

Pinxit answered 20/11, 2017 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.