Skip to content

Suggestion for enhancing Base.Fix to fix a keyword argument #59375

@sgaure

Description

@sgaure

I suggest to enhance the new Base.Fix to handle fixing keyword arguments.

I think it would be fairly easy, something like this, which implements Fix{:kw}(fun, x) to work like (y...) -> fun(y...; kw=x):

struct Fix{N,F,T} <: Function
    f::F
    x::T

    function Fix{N}(f::F, x) where {N,F}
        if N isa Symbol
            return new{Val{N}, _stable_typeof(f), _stable_typeof(x)}(f, x)
        end
        ... as before ...
    end
end

# and the additional:
function (f::Fix{Val{S},F,T})(args...; kws...) where {S,F,T}
    narg = NamedTuple{(S,), Tuple{T}}(f.x)
    return f.f(args...; narg..., kws...)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requestskeyword argumentsf(x; keyword=arguments)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions