Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-Function args to retry #30382

Merged
merged 7 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ New library functions
Standard library changes
------------------------

* `retry` supports arbitrary callable objects ([#30382]).

#### LinearAlgebra

Expand All @@ -36,3 +37,4 @@ Deprecated or removed

<!--- generated by NEWS-update.jl: -->
[#29998]: https://github.com/JuliaLang/julia/issues/29998
[#30382]: https://github.com/JuliaLang/julia/issues/30382
4 changes: 2 additions & 2 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ length(ebo::ExponentialBackOff) = ebo.n
eltype(::Type{ExponentialBackOff}) = Float64

"""
retry(f::Function; delays=ExponentialBackOff(), check=nothing) -> Function
retry(f; delays=ExponentialBackOff(), check=nothing) -> Function

Return an anonymous function that calls function `f`. If an exception arises,
`f` is repeatedly called again, each time `check` returns `true`, after waiting the
Expand All @@ -222,7 +222,7 @@ retry(http_get, check=(s,e)->e.status == "503")(url)
retry(read, check=(s,e)->isa(e, IOError))(io, 128; all=false)
```
"""
function retry(f::Function; delays=ExponentialBackOff(), check=nothing)
function retry(f; delays=ExponentialBackOff(), check=nothing)
(args...; kwargs...) -> begin
y = iterate(delays)
while y !== nothing
Expand Down
3 changes: 3 additions & 0 deletions test/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ end
foo_kwargs(x; y=5) = x + y
@test retry(foo_kwargs)(3) == 8
@test retry(foo_kwargs)(3; y=4) == 7

# non-Functions
@test retry(Float64)(1) === 1.0
end