Custom Compat.jl Block

I wrote a function to collect an iterator into a tuple:

collect_tuple(itr) = _collect_tuple(Iterators.peel(itr))
_collect_tuple(peel_return) = _collect_tuple(peel_return...)
_collect_tuple(::Nothing) = ()
_collect_tuple(val, rest::Iterators.Rest) = (val, collect_tuple(rest)...)

It works in Julia 1.9, but fails in 1.6 because, instead of returning nothing, calling Iterators.peel on an empty container fails on a BoundsError. I’d like to maintain LTS compatibility, so I’m trying to come up with a way to handle this change.

I first looked at Compat.jl, but this case isn’t an included function. Another option would be to add a try..catch and check for a BoundsError. I can easily leave this in all versions since this isn’t a hot part of the code, but on principle, I’d love to drop the error check for Julia versions that don’t need it.

Is there anything like

@compat begin

:default begin
    some code...
v"1.6" begin
   slightly different code...


that I can do?

@static if VERSION ≥ v"1.9"
   # do new Julia stuff here
    # do pre-Julia 1.9 stuff
julia> VERSION

julia> VERSION ≥ v"1.9"
1 Like