# How to execute code depending on julia version

**URL:** <https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029>\
**Category:** General Usage\
**Tags:** versions\
**Created:** [January 22, 2022, 11:23am UTC](https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029 "2022-01-22T11:23:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgctw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bgctw/32/22050_2.png) [@bgctw](https://discourse.julialang.org/u/bgctw)\
**Post date:** [January 22, 2022, 11:23am UTC](https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029/1 "2022-01-22T11:23:07Z")

</div>

How can I conditionally execute some code, depending on what julia version it is running on?

Specifically I test for type stability using [@inferred](https://docs.julialang.org/en/v1/stdlib/Test/#Test.@inferred).  
For some code this succeeds in Julia 1.7 but fails in Julia 1.6. Instead of removing the `@inferred` entirely, I would like to test the Julia version and include the code line with `@inferred` only if running on version \>=1.7.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [January 22, 2022, 1:26pm UTC](https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029/2 "2022-01-22T13:26:11Z")

</div>

```julia
julia> VERSION
v"1.7.0"

julia> fieldnames(typeof(VERSION))
(:major, :minor, :patch, :prerelease, :build)

julia> Int.([VERSION.major, VERSION.minor])
2-element Vector{Int64}:
 1
 7

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 22, 2022, 2:05pm UTC](https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029/3 "2022-01-22T14:05:12Z")

</div>

> [@bgctw](#):
>
> How can I conditionally execute some code, depending on what julia version it is running on?

```julia
@static if VERSION ≥ v"1.7"
    # do something
end

```

(The reason for the `@static` is so that this check occurs at compile time, which is sometimes important, e.g. if you want to `ccall` a function that doesn’t exist before a certain version then you want to strip it out before compilation.)

> [@Bardo](#):
>
> `Int.([VERSION.major, VERSION.minor])`

You should almost never have to look inside the fields of a [`VersionNumber`](https://docs.julialang.org/en/v1/base/base/#Base.VersionNumber) (`== typeof(VERSION)`) object. Just do comparisons with `v"..."` strings as above. See also the manual on [version number literals](https://docs.julialang.org/en/v1/manual/strings/#man-version-number-literals).

(I noticed that the `VersionNumber` docs don’t talk about comparisons, so I created a PR improving them: [julia#43893](https://github.com/JuliaLang/julia/pull/43893).)

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [January 22, 2022, 3:24pm UTC](https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029/4 "2022-01-22T15:24:07Z")

</div>

What’s the “right” way to write a check like `VERSION >= v"1.8"`, but that also returns true for nightlies/pre-releases of that julia version?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 22, 2022, 5:18pm UTC](https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029/5 "2022-01-22T17:18:52Z")

</div>

> [@aplavin](#):
>
> What’s the “right” way to write a check like `VERSION >= v"1.8"` , but that also returns true for nightlies/pre-releases of that julia version?

`VERSION >= v"1.8-"`
