Skip to content

Commit dd4fe66

Browse files
EssamWisamjuliohm
andauthored
Add reapply, revert and Base.inv fallbacks (#3)
* Add reapply, revert and Base.inv fallbacks * Small fix to reapply test * fix infinite loop and not implemented error * improve error throwing for revert Co-authored-by: Júlio Hoffimann <[email protected]> * improve error throwing for revert Co-authored-by: Júlio Hoffimann <[email protected]> * improve error throwing for invert Co-authored-by: Júlio Hoffimann <[email protected]> * improve tests * Update test/runtests.jl * change testsets to normal tests * attempt to fix broken CI tests * Apply suggestions from code review * Update test/runtests.jl * Apply suggestions from code review * Apply suggestions * Update test/runtests.jl --------- Co-authored-by: Júlio Hoffimann <[email protected]>
1 parent 1e16439 commit dd4fe66

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/interface.jl

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ trait can be evaluated directly at any object.
1616
"""
1717
abstract type Transform end
1818

19-
"""
20-
assertions(transform)
21-
22-
Returns a list of assertion functions for the `transform`. An assertion
23-
function is a function that takes an object as input and checks if the
24-
object is valid for the `transform`.
25-
"""
26-
function assertions end
27-
2819
"""
2920
isrevertible(transform)
3021
@@ -51,6 +42,15 @@ See also [`isrevertible`](@ref).
5142
"""
5243
function isinvertible end
5344

45+
"""
46+
assertions(transform)
47+
48+
Returns a list of assertion functions for the `transform`. An assertion
49+
function is a function that takes an object as input and checks if the
50+
object is valid for the `transform`.
51+
"""
52+
function assertions end
53+
5454
"""
5555
prep = preprocess(transform, object)
5656
@@ -80,19 +80,16 @@ function revert end
8080
"""
8181
newobject = reapply(transform, object, cache)
8282
83-
Reapply the `transform` to (a possibly different) `object` using a `cache`
84-
that was created with a previous [`apply`](@ref) call.
83+
Reapply the `transform` to (a possibly different) `object`
84+
using a `cache` that was created with a previous [`apply`](@ref)
85+
call. Fallback to [`apply`](@ref) without using the `cache`.
8586
"""
8687
function reapply end
8788

8889
# --------------------
8990
# TRANSFORM FALLBACKS
9091
# --------------------
9192

92-
assertions(transform::Transform) =
93-
assertions(typeof(transform))
94-
assertions(::Type{<:Transform}) = []
95-
9693
isrevertible(transform::Transform) =
9794
isrevertible(typeof(transform))
9895
isrevertible(::Type{<:Transform}) = false
@@ -101,8 +98,13 @@ isinvertible(transform::Transform) =
10198
isinvertible(typeof(transform))
10299
isinvertible(::Type{<:Transform}) = false
103100

101+
assertions(transform::Transform) = []
102+
104103
preprocess(transform::Transform, object) = nothing
105104

105+
reapply(transform::Transform, object, cache) =
106+
apply(transform, object) |> first
107+
106108
(transform::Transform)(object) =
107109
apply(transform, object) |> first
108110

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@ using Test
77
@test inv(Identity()) == Identity()
88
@test inv(Identity() Identity()) == Identity()
99
@test (Identity() Identity()) == Identity()
10+
11+
# test fallbacks
12+
struct TestTransform <: TransformsBase.Transform end
13+
TransformsBase.apply(::TestTransform, x) = x, nothing
14+
T = TestTransform()
15+
@test !TransformsBase.isrevertible(T)
16+
@test !TransformsBase.isinvertible(T)
17+
@test TransformsBase.assertions(T) |> isempty
18+
@test TransformsBase.preprocess(T, nothing) |> isnothing
19+
@test TransformsBase.reapply(T, 1, nothing) == 1
1020
end

0 commit comments

Comments
 (0)