From 244b9e260b19252dab62a5c32f58b644d3b7068c Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Mon, 22 Jul 2024 06:45:46 -0400 Subject: [PATCH] support more functions in constructorof (#89) Now works with regular functions and non-closure anonymous functions as well. --- Project.toml | 2 +- src/functions.jl | 2 ++ test/runtests.jl | 6 +++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 6a9b1f6..aac34fb 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/functions.jl b/src/functions.jl index 739aacd..ee8d39d 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -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...)) diff --git a/test/runtests.jl b/test/runtests.jl index 9ef66c7..f4e68ff 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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