Skip to content
Open
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
17 changes: 9 additions & 8 deletions src/ScopedValues.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ScopedValues

export ScopedValue, with, @with, ScopedFunctor
export ScopedValue, with, @with, ScopedThunk

if isdefined(Base, :ScopedValues)

Expand Down Expand Up @@ -237,21 +237,22 @@ include("payloadlogger.jl")
end # isdefined

"""
ScopedFunctor(f)
ScopedThunk(f)

Create a functor that records the current dynamic scope, i.e. all current
`ScopedValue`s, along with `f`. When the functor is invoked, it runs `f`
Create a callable that records the current dynamic scope, i.e. all current
`ScopedValue`s, along with `f`. When the callable is invoked, it runs `f`
in the recorded dynamic scope.
"""
struct ScopedFunctor{F}
struct ScopedThunk{F}
f::F
scope::Union{Nothing, Scope}

ScopedFunctor{F}(f) where {F} = new{F}(f, current_scope())
ScopedThunk{F}(f) where {F} = new{F}(f, current_scope())
end
ScopedFunctor(f) = ScopedFunctor{typeof(f)}(f)
(sf::ScopedFunctor)() = @enter_scope sf.scope sf.f()
ScopedThunk(f) = ScopedThunk{typeof(f)}(f)
(sf::ScopedThunk)() = @enter_scope sf.scope sf.f()

@deprecate scoped with
@deprecate ScopedFunctor ScopedThunk

end # module ScopedValues
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ end
@test ret == 1.23
end

@testset "ScopedFunctor" begin
@testset "ScopedThunk" begin
function check_svals()
@test sval[] == 8
@test sval_float[] == 8.0
end
sf = nothing
@with sval=>8 sval_float=>8.0 begin
sf = ScopedFunctor(check_svals)
sf = ScopedThunk(check_svals)
end
sf()
@with sval=>8 sval_float=>8.0 begin
sf2 = ScopedFunctor{Function}(check_svals)
sf2 = ScopedThunk{Function}(check_svals)
end
sf2()
end
Loading