Skip to content

Commit 5e4b720

Browse files
authored
Deprecate calling thunks like functions (#375)
1 parent 3f3019f commit 5e4b720

4 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/deprecated.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ extern(x::NotImplemented) = (Base.depwarn(EXTERN_DEPRECATION, :extern); throw(No
4848

4949
@inline extern(x::AbstractThunk) = (Base.depwarn(EXTERN_DEPRECATION, :extern); return extern(unthunk(x)))
5050

51-
52-
53-
51+
for T in (:Thunk, :InplaceableThunk)
52+
@eval function (x::$T)()
53+
Base.depwarn("`(x::" * string($T) * ")()` is deprecated, use `unthunk(x)`", Symbol(:call_, $(T)))
54+
return unthunk(x)
55+
end
56+
end

src/differentials/thunks.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ A thunk is a deferred computation.
148148
It wraps a zero argument closure that when invoked returns a differential.
149149
`@thunk(v)` is a macro that expands into `Thunk(()->v)`.
150150
151-
Calling a thunk, calls the wrapped closure.
152-
If you are unsure if you have a `Thunk`, call [`unthunk`](@ref) which is a no-op when the
151+
To evaluate the wrapped closure, call [`unthunk`](@ref) which is a no-op when the
153152
argument is not a `Thunk`.
154153
155154
```jldoctest
@@ -159,7 +158,7 @@ Thunk(var"#4#6"())
159158
julia> t()
160159
Thunk(var"#5#7"())
161160
162-
julia> t()()
161+
julia> unthunk(t())
163162
3
164163
```
165164
@@ -187,8 +186,7 @@ struct Thunk{F} <: AbstractThunk
187186
f::F
188187
end
189188

190-
(x::Thunk)() = x.f()
191-
@inline unthunk(x::Thunk) = x()
189+
@inline unthunk(x::Thunk) = x.f()
192190

193191
Base.show(io::IO, x::Thunk) = print(io, "Thunk($(repr(x.f)))")
194192

@@ -210,7 +208,6 @@ struct InplaceableThunk{T<:Thunk,F} <: AbstractThunk
210208
end
211209

212210
unthunk(x::InplaceableThunk) = unthunk(x.val)
213-
(x::InplaceableThunk)() = unthunk(x)
214211

215212
function Base.show(io::IO, x::InplaceableThunk)
216213
return print(io, "InplaceableThunk($(repr(x.val)), $(repr(x.add!)))")

test/deprecated.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ end
2222
E = ChainRulesCore.NotImplementedException
2323
@test_throws E extern(ni)
2424
end
25+
26+
27+
@testset "Deprecated: calling thunks should call inner function" begin
28+
@test_deprecated (@thunk(3))() == 3
29+
@test_deprecated (@thunk(@thunk(3)))() isa Thunk
30+
end

test/differentials/thunks.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
@test unthunk(@thunk(@thunk(3))) isa Thunk
2828
end
2929

30-
@testset "calling thunks should call inner function" begin
31-
@test (@thunk(3))() == 3
32-
@test (@thunk(@thunk(3)))() isa Thunk
33-
end
34-
3530
@testset "erroring thunks should include the source in the backtrack" begin
3631
expected_line = (@__LINE__) + 2 # for testing it is at right palce
3732
try

0 commit comments

Comments
 (0)