From 25fc0c6911945961b784a5c373b94ce3ebcb2aaf Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 10 Feb 2023 19:33:27 +0300 Subject: [PATCH 1/2] fix constructorof(Expr) --- src/nonstandard.jl | 3 +++ test/runtests.jl | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/nonstandard.jl b/src/nonstandard.jl index 640306e..996eae6 100644 --- a/src/nonstandard.jl +++ b/src/nonstandard.jl @@ -52,3 +52,6 @@ constructorof(::Type{<:LinearAlgebra.Tridiagonal}) = tridiagonal_constructor linrange_constructor(start, stop, len, lendiv) = LinRange(start, stop, len) constructorof(::Type{<:LinRange}) = linrange_constructor + +### Expr: args get splatted +constructorof(::Type{<:Expr}) = (head, args) -> Expr(head, args...) diff --git a/test/runtests.jl b/test/runtests.jl index 0cfb291..0916b7b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -194,6 +194,10 @@ end @inferred constructorof(typeof(lr1))(getfields(lr2)...) end + @testset "Expr" begin + e = :(a + b) + @test e == @inferred constructorof(typeof(e))(getfields(e)...) + end end @testset "Anonymous function constructors" begin From 16e8066d06953503a80ce0553bda98ea7b34d48f Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 17 Feb 2023 11:49:19 +0300 Subject: [PATCH 2/2] fix type-stability on old Julias --- src/nonstandard.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/nonstandard.jl b/src/nonstandard.jl index 996eae6..2ba642d 100644 --- a/src/nonstandard.jl +++ b/src/nonstandard.jl @@ -54,4 +54,5 @@ linrange_constructor(start, stop, len, lendiv) = LinRange(start, stop, len) constructorof(::Type{<:LinRange}) = linrange_constructor ### Expr: args get splatted -constructorof(::Type{<:Expr}) = (head, args) -> Expr(head, args...) +# ::Expr annotation is to make it type-stable on Julia 1.3- +constructorof(::Type{<:Expr}) = (head, args) -> Expr(head, args...)::Expr