Skip to content

Commit

Permalink
support more functions in constructorof
Browse files Browse the repository at this point in the history
Now works with regular functions and non-closure anonymous functions as well.
  • Loading branch information
aplavin committed Jul 22, 2024
1 parent d533216 commit 184668c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ struct FunctionConstructor{F} end
_isgensym(s::Symbol) = occursin("#", string(s))

@generated function (fc::FunctionConstructor{F})(args...) where F
isempty(args) && return Expr(:new, F)

T = getfield(parentmodule(F), nameof(F))
# We assume all gensym names are anonymous functions
_isgensym(nameof(F)) || return :($T(args...))
Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ end
end
end

@testset "Anonymous function constructors" begin
@testset "default function constructors" begin
function multiplyer(a, b)
x -> x * a * b
end
Expand All @@ -259,6 +259,10 @@ end
multbc = @inferred constructorof(typeof(mult23))("b", "c")
@inferred multbc("a")
@test multbc("a") == "abc"

@test (@inferred constructorof(typeof(sin))()) === sin
f = x -> x^2
@test (@inferred constructorof(typeof(f))()) === f
end

struct Adder{V} <: Function
Expand Down

0 comments on commit 184668c

Please sign in to comment.