Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macro for conditionally choosing code based on Julia version #131

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,14 @@ if VERSION < v"0.4.0-dev+6578"
export ≈, ≉
end

macro julia_geq(ex)
(Base.Meta.isexpr(ex, :if) && length(ex.args) == 3) ||
throw(ArgumentError("invalid syntax"))
version = ex.args[1]
(Base.Meta.isexpr(version, :macrocall) && length(version.args) == 2 && version.args[1] === symbol("@v_str")) ||
throw(ArgumentError("invalid syntax"))
VERSION >= convert(VersionNumber, version.args[2]) ? esc(ex.args[2]) : esc(ex.args[3])
end
export @julia_geq

end # module
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,9 @@ Compat.@irrational mathconst_one 1.0 big(1.)
@test [1,2,3] ≈ [1,2,3+1e-9]
@test [0,1] ≈ [1e-9, 1]
@test [0,1] ≉ [1e-3, 1]

passed = false
@julia_geq v"0.1-" ? (passed = true) : error("@julia_geq test failed")
@test passed
passed = false
@julia_geq v"100" ? error("@julia_geq test failed") : (passed = true)