diff --git a/NEWS.md b/NEWS.md index 7b50874313bee..71fc55d2ed251 100644 --- a/NEWS.md +++ b/NEWS.md @@ -33,10 +33,11 @@ New library functions Standard library changes ------------------------ - * The `extrema` function now accepts a function argument in the same manner as `minimum` and - `maximum` ([#30323]). - * `hasmethod` can now check for matching keyword argument names ([#30712]). - * `startswith` and `endswith` now accept a `Regex` for the second argument ([#29790]). +* The `extrema` function now accepts a function argument in the same manner as `minimum` and + `maximum` ([#30323]). +* `hasmethod` can now check for matching keyword argument names ([#30712]). +* `startswith` and `endswith` now accept a `Regex` for the second argument ([#29790]). +* `retry` supports arbitrary callable objects ([#30382]). #### LinearAlgebra @@ -86,6 +87,7 @@ Deprecated or removed [#30323]: https://github.com/JuliaLang/julia/issues/30323 [#30349]: https://github.com/JuliaLang/julia/issues/30349 [#30372]: https://github.com/JuliaLang/julia/issues/30372 +[#30382]: https://github.com/JuliaLang/julia/issues/30382 [#30583]: https://github.com/JuliaLang/julia/issues/30583 [#30584]: https://github.com/JuliaLang/julia/issues/30584 [#30593]: https://github.com/JuliaLang/julia/issues/30593 diff --git a/base/error.jl b/base/error.jl index 65b76d4823b87..bc04f89d6f8c3 100644 --- a/base/error.jl +++ b/base/error.jl @@ -221,13 +221,16 @@ 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 number of seconds specified in `delays`. `check` should input `delays`'s current state and the `Exception`. +!!! compat "Julia 1.2" + Before Julia 1.2 this signature was restricted to `f::Function`. + # Examples ```julia retry(f, delays=fill(5.0, 3)) @@ -237,7 +240,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 diff --git a/test/error.jl b/test/error.jl index 741ae762acbe3..bb97a0e66ed0b 100644 --- a/test/error.jl +++ b/test/error.jl @@ -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