Julia has a built-in implementation of semantic versioning exposed through the VersionNumber
type.
To construct a VersionNumber
as a literal, the @v_str
string macro can be used:
julia> vers = v"1.2.0"
v"1.2.0"
Alternatively, one can call the VersionNumber
constructor; note that the constructor accepts up to five arguments, but all except the first are optional.
julia> vers2 = VersionNumber(1, 1)
v"1.1.0"
Version numbers can be compared using comparison operators, and thus can be sorted:
julia> vers2 < vers
true
julia> v"1" < v"0"
false
julia> sort([v"1.0.0", v"1.0.0-dev.100", v"1.0.1"])
3-element Array{VersionNumber,1}:
v"1.0.0-dev.100"
v"1.0.0"
v"1.0.1"
Version numbers are used in several places across Julia. For instance, the VERSION
constant is a VersionNumber
:
julia> VERSION
v"0.5.0"
This is commonly used for conditional code evaluation, depending on the Julia version. For example, to run different code on v0.4 and v0.5, one can do
if VERSION < v"0.5"
println("v0.5 prerelease, v0.4 or older")
else
println("v0.5 or newer")
end
Each installed package is also associated with a current version number:
julia> Pkg.installed("StatsBase")
v"0.9.0"