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 Base.FixKwargs (a la Base.Fix1) for more introspectable broadcasts #36093

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,8 @@ macro __dot__(x)
esc(__dot__(x))
end

@inline broadcasted_kwsyntax(f, args...; kwargs...) = broadcasted((args...)->f(args...; kwargs...), args...)
@inline broadcasted_kwsyntax(f, args...; kwargs...) =
broadcasted(Base.FixKwargs(f, kwargs), args...)
@inline function broadcasted(f, args...)
args′ = map(broadcastable, args)
broadcasted(combine_styles(args′...), f, args′...)
Expand Down
27 changes: 27 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,33 @@ end

(f::Fix2)(y) = f.f(y, f.x)

"""
Base.FixKwargs(f; kwargs...)
Base.FixKwargs(f, kwargs)

A type representing a partially-applied version of the keyword argument function
`f`, with all keyword arguments fixed to those passed. In other words,
`FixKwargs(f; kwargs...)` behaves similarly to `(args...) -> f(args...; kwargs...)`.

The callables of type `FixKwargs` are created when function calls with
keyword arguments are used in the dot-call syntax. The dot-call
sub-expression `f.(args...; kwargs...)` is lowered to a form that is
equivalent to `broadcasted(FixKwargs(f, kwargs), args...)`.

# Properties
- `f::F`: a callable
- `kwargs::K`: the keyword arguments passed to `f`
"""
struct FixKwargs{F,K} <: Function
f::F
kwargs::K
end

FixKwargs(::Type{T}, kwargs::K) where {T,K} = FixKwargs{Type{T},K}(T, kwargs)
FixKwargs(f; kwargs...) = FixKwargs(f, kwargs)

(f::FixKwargs)(args...) = f.f(args...; f.kwargs...)

"""
isequal(x)

Expand Down
24 changes: 24 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ let x = [1:4;]
@test sin.(f17300kw.(x, y=1)) == sin.(f17300kw.(x; y=1)) == sin.(x .+ 1)
end

function lazybc end
struct LazyBC{T}
value::T
end
Broadcast.broadcasted(::typeof(lazybc), x) = LazyBC(x)
Broadcast.materialize(x::LazyBC) = x.value

@testset "FixKwargs" begin
function f end
bc = lazybc.(f.(1, 2, a = 3, b = 4))
@test bc.f isa Base.FixKwargs
@test bc.f.f === f
@test (; bc.f.kwargs...) === (a = 3, b = 4)
@test bc.args == (1, 2)
end

struct TypeWithKwargs end
TypeWithKwargs(a, args...; kwargs...) = TypeWithKwargs()

@testset "type inference with a type with kwargs" begin
f() = last.(tuple.([1], TypeWithKwargs.(1, 2; a = 3, b = 4)))[1]
@test @inferred(f()) === TypeWithKwargs()
end

# issue #23236
let X = [[true,false],[false,true]]
@test [.!x for x in X] == [[false,true],[true,false]]
Expand Down