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

Conversation

simonster
Copy link
Member

Some things, like uses of the old FFT API, are difficult/never exported/not used widely enough to define in Compat. However, when it's necessary to select different code based on the Julia version within a function, comparing VERSION directly is a bad idea because it incurs a runtime cost and also potentially screws with type inference. This PR defines a @julia_geq macro that works like:

function f()
    x = @julia_geq v"0.4.0-dev+6632" ? 1 : 2.0
    x + 1
end

It can also be used with blocks:

function f()
    @julia_geq v"0.4.0-dev+6632" ? begin
        ...
    end : begin
        ...
     end
end

I'm open to bikeshedding both the name and the syntax, but since I'm already using something like this in DSP, I figured it might be useful enough to include here.

@simonster
Copy link
Member Author

Also worth referring to JuliaLang/julia#5892 and JuliaLang/julia#7449. In particular, @one-more-minute's macro from JuliaLang/julia#5892 is another possible approach.

@stevengj
Copy link
Member

For syntax, how about @VERSION >= v"...."? This should be valid since @VERSION does not conflict with VERSION.

@stevengj
Copy link
Member

Of course, it would be better if the compiler could statically eval VersionNumber comparisons, but that seems like another case for JuliaLang/julia#414.

@simonster
Copy link
Member Author

Unfortunately you can't start a macro argument with a comparison operator because they aren't unary operators and so that won't parse, e.g.:

julia> :(>= v"0.4" ? 1 : 2)
ERROR: syntax: ">=" is not a unary operator

@TotalVerb
Copy link
Contributor

This is obsoleted by JuliaLang/julia#16219, which was just merged. Now

@static if VERSION [ ... ] end

can be used.

The @static macro can be provided (and exported) from Compat itself on 0.3 and 0.4.

@tkelman tkelman closed this in #204 May 23, 2016
martinholters pushed a commit to martinholters/Compat.jl that referenced this pull request Jul 13, 2016
Add type stable copy!(::DataArray, ::Integer, ...) methods
@giordano giordano deleted the sjk/julia-geq branch January 20, 2024 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants