File tree Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 1
1
name = " ChainRulesCore"
2
2
uuid = " d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3
- version = " 0.2.0 "
3
+ version = " 0.2.1-DEV "
4
4
5
5
[compat ]
6
6
julia = " ^1.0"
Original file line number Diff line number Diff line change @@ -181,6 +181,24 @@ Base.iterate(::One, ::Any) = nothing
181
181
Thunk(()->v)
182
182
A thunk is a deferred computation.
183
183
It wraps a zero argument closure that when invoked returns a differential.
184
+
185
+ Calling that thunk, calls the wrapped closure.
186
+ `extern`ing thunks applies recursively, it also externs the differial that the closure returns.
187
+ If you do not want that, then simply call the thunk
188
+
189
+ ```
190
+ julia> t = @thunk(@thunk(3))
191
+ Thunk(var"##7#9"())
192
+
193
+ julia> extern(t)
194
+ 3
195
+
196
+ julia> t()
197
+ Thunk(var"##8#10"())
198
+
199
+ julia> t()()
200
+ 3
201
+ ```
184
202
"""
185
203
struct Thunk{F} <: AbstractDifferential
186
204
f:: F
@@ -190,7 +208,8 @@ macro thunk(body)
190
208
return :(Thunk (() -> $ (esc (body))))
191
209
end
192
210
193
- @inline extern (x:: Thunk ) = x. f ()
211
+ (x:: Thunk )() = x. f ()
212
+ @inline extern (x:: Thunk ) = extern (x ())
194
213
195
214
Base. Broadcast. broadcastable (x:: Thunk ) = broadcastable (extern (x))
196
215
206
225
end
207
226
208
227
Base. conj (x:: Thunk ) = @thunk (conj (extern (x)))
228
+
229
+ Base. show (io:: IO , x:: Thunk ) = println (io, " Thunk($(repr (x. f)) )" )
Original file line number Diff line number Diff line change 49
49
@test conj (o) == o
50
50
end
51
51
52
+ @testset " Thunk" begin
53
+ @test @thunk (3 ) isa Thunk
54
+
55
+ @testset " show" begin
56
+ rep = repr (Thunk (rand))
57
+ @test occursin (r" Thunk\( .*rand.*\) " , rep)
58
+ end
59
+
60
+ @testset " Externing" begin
61
+ @test extern (@thunk (3 )) == 3
62
+ @test extern (@thunk (@thunk (3 ))) == 3
63
+ end
64
+
65
+ @testset " calling thunks should call inner function" begin
66
+ @test (@thunk (3 ))() == 3
67
+ @test (@thunk (@thunk (3 )))() isa Thunk
68
+ end
69
+ end
70
+
52
71
@testset " No ambiguities in $f " for f in (+ , * )
53
72
# We don't use `Test.detect_ambiguities` as we are only interested in
54
73
# the +, and * operations. We also would catch any that are unrelated
You can’t perform that action at this time.
0 commit comments