Skip to content

Commit 244b9e2

Browse files
authored
support more functions in constructorof (#89)
Now works with regular functions and non-closure anonymous functions as well.
1 parent d533216 commit 244b9e2

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ConstructionBase"
22
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
33
authors = ["Takafumi Arakaki", "Rafael Schouten", "Jan Weidner"]
4-
version = "1.5.5"
4+
version = "1.5.6"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/functions.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ struct FunctionConstructor{F} end
66
_isgensym(s::Symbol) = occursin("#", string(s))
77

88
@generated function (fc::FunctionConstructor{F})(args...) where F
9+
isempty(args) && return Expr(:new, F)
10+
911
T = getfield(parentmodule(F), nameof(F))
1012
# We assume all gensym names are anonymous functions
1113
_isgensym(nameof(F)) || return :($T(args...))

test/runtests.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ end
246246
end
247247
end
248248

249-
@testset "Anonymous function constructors" begin
249+
@testset "default function constructors" begin
250250
function multiplyer(a, b)
251251
x -> x * a * b
252252
end
@@ -259,6 +259,10 @@ end
259259
multbc = @inferred constructorof(typeof(mult23))("b", "c")
260260
@inferred multbc("a")
261261
@test multbc("a") == "abc"
262+
263+
@test (@inferred constructorof(typeof(sin))()) === sin
264+
f = x -> x^2
265+
@test (@inferred constructorof(typeof(f))()) === f
262266
end
263267

264268
struct Adder{V} <: Function

0 commit comments

Comments
 (0)