# Backporting new Julia features in old versions of Julia

**URL:** https://discourse.julialang.org/t/backporting-new-julia-features-in-old-versions-of-julia/98144
**Category:** General Usage
**Tags:** question
**Created:** [April 30, 2023, 10:46pm UTC](https://discourse.julialang.org/t/backporting-new-julia-features-in-old-versions-of-julia/98144 "2023-04-30T22:46:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)
#### Post date: [April 30, 2023, 10:46pm UTC](https://discourse.julialang.org/t/backporting-new-julia-features-in-old-versions-of-julia/98144/1 "2023-04-30T22:46:26Z")

</div>

Let’s say that there is a simple new feature in Julia 1.9 that I would like to use in my library. However, for various reasons I need to keep my library compatible with older Julia versions as well. Is there a standard way I can backport this new Julia feature, without having to copy the code myself?

As an example, I want to use `Returns(1)` in my Julia library. But I also want to allow users to download the latest version of my library for Julia 1.6 (say, a group in industry wants to use recent patches of my library, but also uses LTS Julia for whatever internal reason).

So, rather than:

1. Avoiding new features, or
2. maintaining two versions of my code,

I just copied the source code into my library:

```julia
if VERSION < v"1.7"
    @eval struct Returns{V} <: Function
        value::V
        Returns{V}(value) where {V} = new{V}(value)
        Returns(value) = new{typeof(value)}(value)
    end
    @eval (obj::Returns)(@nospecialize(args...); @nospecialize(kw...)) = obj.value
end

```

And used `Returns(1)` as normal in my code. I have started accumulating a few other features which I wanted to use: `randn(::Type{BigFloat})` (copied), `@inline(f(x))` (no-op on earlier versions of Julia).

But this seems like it would be a common issue, so perhaps there is a better way. For example, while you may correctly point out potential issues with this: there is `from __future__ import print_function` in Python, for importing new features in backwards compatible code.

Perhaps there is some Julia library I have missed which defines many of the new features in such a way that I can:

1. Import them on old versions of Julia, or
2. No-op on new versions of Julia?

Like:

```julia
import Future: Returns, @inline, randn

```

which would import new definitions of these on old versions, and do nothing on new versions. (Of course, it wouldn’t need to backport _everything_, just the most popular new features which are otherwise backwards-compatible)

Is there something like this out there?

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [April 30, 2023, 10:49pm UTC](https://discourse.julialang.org/t/backporting-new-julia-features-in-old-versions-of-julia/98144/2 "2023-04-30T22:49:44Z")

</div>

> **[GitHub - JuliaLang/Compat.jl: Compatibility across Julia versions](https://github.com/JuliaLang/Compat.jl)**
>
> Compatibility across Julia versions. Contribute to JuliaLang/Compat.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)
#### Post date: [April 30, 2023, 10:50pm UTC](https://discourse.julialang.org/t/backporting-new-julia-features-in-old-versions-of-julia/98144/3 "2023-04-30T22:50:54Z")

</div>

Amazing. Thanks!

---

<div class="post-metadata">

### Author: ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)
#### Post date: [May 5, 2023, 7:20pm UTC](https://discourse.julialang.org/t/backporting-new-julia-features-in-old-versions-of-julia/98144/4 "2023-05-05T19:20:37Z")

</div>

Side note: I think `Compat.jl` is an ambiguity-generating package name.

I realized that I’ve actually been told about `Compat.jl` before, but I had always thought they meant to just raise my `[compat]` setting in `Project.toml` to get the new features (i.e., set `julia="1.9"`), not that it was an entirely separate package.

I think I had thought that `Compat.jl` was the `Pkg.jl` subpackage that handled the `compat` key.
