From ef6a1c9ea39a594db1b6454aa100521a3d3557f8 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Fri, 14 Aug 2015 15:23:12 -0400 Subject: [PATCH] Add macro for conditionally choosing code based on Julia version --- src/Compat.jl | 10 ++++++++++ test/runtests.jl | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/Compat.jl b/src/Compat.jl index 56eb02f61..de8f88c91 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 9e6cbf8f7..ae89e7983 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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)