Skip to content

Commit

Permalink
Dispatch on Type instead of using Core.Typeof
Browse files Browse the repository at this point in the history
  • Loading branch information
oschulz committed Jul 2, 2023
1 parent 7f9884c commit 07ac8c9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/inverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ An instance `NoInverse(f)` signifies that `inverse(f)` is not defined.
"""
struct NoInverse{F}
f::F
NoInverse{F}(f) where F = new{F}(f)
NoInverse(f) = new{Core.Typeof(f)}(f)
end
export NoInverse

NoInverse(::Type{F}) where F = NoInverse{Type{F}}(F)

(f::NoInverse)(x) = error("inverse of ", f.f, " is not defined")

inverse(f) = NoInverse(f)
Expand Down
6 changes: 3 additions & 3 deletions src/setinverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Do not construct directly, use [`setinverse(f, invf)`](@ref) instead.
struct FunctionWithInverse{F,InvF} <: Function
f::F
invf::InvF
FunctionWithInverse{F, InvF}(f, invf) where {F, InvF} = new{F, InvF}(f, invf)
FunctionWithInverse(f, invf) = new{Core.Typeof(f),Core.Typeof(invf)}(f, invf)
end

FunctionWithInverse(::Type{F}, invf::InvF) where {F,InvF} = FunctionWithInverse{Type{F},InvF}(F,invf)
FunctionWithInverse(f::F, ::Type{InvF}) where {F,InvF} = FunctionWithInverse{F,Type{InvF}}(f,InvF)
FunctionWithInverse(::Type{F}, ::Type{InvF}) where {F,InvF} = FunctionWithInverse{Type{F},Type{InvF}}(F,InvF)

(f::FunctionWithInverse)(x) = f.f(x)

Expand Down
1 change: 0 additions & 1 deletion test/test_inverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))

@test @inferred(inverse(Complex)) isa NoInverse{Type{Complex}}
@test @inferred(NoInverse(Complex)) isa NoInverse{Type{Complex}}
@test @inferred(NoInverse{Type{Complex}}(Complex)) isa NoInverse{Type{Complex}}

InverseFunctions.test_inverse(inverse, log, compare = ===)

Expand Down
5 changes: 4 additions & 1 deletion test/test_setinverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ using InverseFunctions
@testset "setinverse" begin
@test @inferred(setinverse(Complex, Real)) isa InverseFunctions.FunctionWithInverse{Type{Complex},Type{Real}}
@test @inferred(InverseFunctions.FunctionWithInverse(Complex, Real)) isa InverseFunctions.FunctionWithInverse{Type{Complex},Type{Real}}
@test @inferred(InverseFunctions.FunctionWithInverse{Type{Complex},Type{Real}}(Complex, Real)) isa InverseFunctions.FunctionWithInverse{Type{Complex},Type{Real}}
@test @inferred(InverseFunctions.FunctionWithInverse(Real, identity)) isa InverseFunctions.FunctionWithInverse{Type{Real},typeof(identity)}
@test @inferred(InverseFunctions.FunctionWithInverse(identity, Real)) isa InverseFunctions.FunctionWithInverse{typeof(identity),Type{Real}}
InverseFunctions.test_inverse(setinverse(Complex, Real), 4.2)
InverseFunctions.test_inverse(setinverse(Real, identity), 4.2)
InverseFunctions.test_inverse(setinverse(identity, Real), 4.2)

@test @inferred(setinverse(sin, asin)) === InverseFunctions.FunctionWithInverse(sin, asin)
@test @inferred(setinverse(sin, setinverse(asin, sqrt))) === InverseFunctions.FunctionWithInverse(sin, asin)
Expand Down

2 comments on commit 07ac8c9

@oschulz
Copy link
Collaborator Author

@oschulz oschulz commented on 07ac8c9 Jul 2, 2023

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/86720

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 v0.1.10 -m "<description of version>" 07ac8c97d6fef58a890b55bdda8a6a0b67a68bfa
git push origin v0.1.10

Please sign in to comment.