Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ extern(x::NotImplemented) = (Base.depwarn(EXTERN_DEPRECATION, :extern); throw(No

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




for T in (:Thunk, :InplaceableThunk)
@eval function (x::$T)()
Base.depwarn("`(x::" * string($T) * ")()` is deprecated, use `unthunk(x)`", Symbol(:call_, $(T)))
return unthunk(x)
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just replace both of these with

function (x::AbstractThunk)()
    Base.depwarn("`(x::AbstractThunk)()`` is deprecated, use `unthunk(x)`", :call_AbstractThunk)
    return unthunk(x)
end

?

Also, BlueStyle.jl uses explicit return

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, use AbstractThunk in the depwarn message for both of them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, should have been something like

function (x::T)() where T <: AbstractThunk
    Base.depwarn("`(x::T)()`` is deprecated, use `unthunk(x)`", :call_AbstractThunk)
    return unthunk(x)
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do that - ChainRulesCore is backward compatible with Julia v1.0, and (x::AbstractThunk)() = ... would require >= v1.3, right? And (x::T)() where T <: AbstractThunk = ... even v1.6 won't allow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking 1.0. With the risk of belabouring the idea:

for T in (:Thunk, :InplaceableThunk)
    @eval function (x::$T)()
        Base.depwarn("`(x::" * string($T) * ")()` is deprecated, use `unthunk(x)`", Symbol(:call_, $(T))) 
        return unthunk(x)
    end    
end

should work. Feel free to ignore if you think that the current way is better (but please do add return)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - just updated the PR, using this variant.

9 changes: 3 additions & 6 deletions src/differentials/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ A thunk is a deferred computation.
It wraps a zero argument closure that when invoked returns a differential.
`@thunk(v)` is a macro that expands into `Thunk(()->v)`.

Calling a thunk, calls the wrapped closure.
If you are unsure if you have a `Thunk`, call [`unthunk`](@ref) which is a no-op when the
To evaluate the wrapped closure, call [`unthunk`](@ref) which is a no-op when the
argument is not a `Thunk`.

```jldoctest
Expand All @@ -159,7 +158,7 @@ Thunk(var"#4#6"())
julia> t()
Thunk(var"#5#7"())

julia> t()()
julia> unthunk(t())
3
```

Expand Down Expand Up @@ -187,8 +186,7 @@ struct Thunk{F} <: AbstractThunk
f::F
end

(x::Thunk)() = x.f()
@inline unthunk(x::Thunk) = x()
@inline unthunk(x::Thunk) = x.f()

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

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

unthunk(x::InplaceableThunk) = unthunk(x.val)
(x::InplaceableThunk)() = unthunk(x)

function Base.show(io::IO, x::InplaceableThunk)
return print(io, "InplaceableThunk($(repr(x.val)), $(repr(x.add!)))")
Expand Down
6 changes: 6 additions & 0 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ end
E = ChainRulesCore.NotImplementedException
@test_throws E extern(ni)
end


@testset "Deprecated: calling thunks should call inner function" begin
@test_deprecated (@thunk(3))() == 3
@test_deprecated (@thunk(@thunk(3)))() isa Thunk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test InplaceableThunk?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test InplaceableThunk?

Well, we didn't before, but I can add it.

end
5 changes: 0 additions & 5 deletions test/differentials/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
@test unthunk(@thunk(@thunk(3))) isa Thunk
end

@testset "calling thunks should call inner function" begin
@test (@thunk(3))() == 3
@test (@thunk(@thunk(3)))() isa Thunk
end

@testset "erroring thunks should include the source in the backtrack" begin
expected_line = (@__LINE__) + 2 # for testing it is at right palce
try
Expand Down