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

support more functions in constructorof #89

Merged
merged 2 commits into from
Jul 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ConstructionBase"
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
authors = ["Takafumi Arakaki", "Rafael Schouten", "Jan Weidner"]
version = "1.5.5"
version = "1.5.6"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
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
Loading