Skip to content

Commit

Permalink
support more functions in constructorof (#89)
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 authored Jul 22, 2024
1 parent d533216 commit 244b9e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
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

4 comments on commit 244b9e2

@aplavin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Register Failed
@aplavin, it looks like you are not a publicly listed member/owner in the parent organization (JuliaObjects).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@aplavin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111500

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.6 -m "<description of version>" 244b9e260b19252dab62a5c32f58b644d3b7068c
git push origin v1.5.6

Please sign in to comment.