JuliaLang / Compat.jl

Compatibility across Julia versions
Other
145 stars 117 forks source link

Add `Fix{N}` for fixing a single positional argument at any position #829

Closed MilesCranmer closed 1 month ago

MilesCranmer commented 1 month ago

Adds https://github.com/JuliaLang/julia/pull/54653.

Fixes #828.

julia> using Compat: Fix

julia> map(Fix{3}(muladd, 1.0), 1:3, 4:6)
3-element Vector{Float64}:
  5.0
 11.0
 19.0

Multiple dispatch: This is a bit of an edge case, and presumably many functions in Compat are like this, but since Fix1 is broadly used:

Note that even with this PR, Compat.Fix{1} is not the same as Base.Fix1 on Julia versions earlier than 1.12.0-DEV.966. Therefore, if you wish to use this as a way to dispatch on Fix{N} for libraries compatible with Julia earlier than v1.12, you should declare a function both for Compat.Fix{1} as well as Base.Fix1. You can do this with a

@static if !isdefined(Base, :Fix)
    #= Declare additional methods for Compat.Fix =#
end
MilesCranmer commented 1 month ago

@martinholters would you be willing to take a look

MilesCranmer commented 1 month ago

Should this be documented somewhere more findable than the PR description?

I’ll add it to the docs for the function

MilesCranmer commented 1 month ago

Done.

MilesCranmer commented 1 month ago

Great, thanks!