Skip to content

Commit 6e04e3a

Browse files
authored
add Returns (#751)
1 parent d79d0fc commit 6e04e3a

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "3.34.0"
3+
version = "3.35.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ changes in `julia`.
5454

5555
## Supported features
5656

57+
* `Returns(value)` returns `value` for any arguments ([#39794]) (since Compat 3.35)
58+
5759
* The function `current_exceptions()` has been added to get the current
5860
exception stack. Julia-1.0 lacks runtime support for full execption stacks,
5961
so we return only the most recent exception in that case. ([#29901]) (since
@@ -270,3 +272,4 @@ Note that you should specify the correct minimum version for `Compat` in the
270272
[#35316]: https://github.com/JuliaLang/julia/pull/35316
271273
[#41076]: https://github.com/JuliaLang/julia/pull/41076
272274
[#34331]: https://github.com/JuliaLang/julia/pull/34331
275+
[#39794]: https://github.com/JuliaLang/julia/pull/39794

src/Compat.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,25 @@ if VERSION < v"1.7.0-DEV.1106"
11021102
export current_exceptions
11031103
end
11041104

1105+
# https://github.com/JuliaLang/julia/pull/39794
1106+
if VERSION < v"1.7.0-DEV.793"
1107+
export Returns
1108+
1109+
struct Returns{V} <: Function
1110+
value::V
1111+
Returns{V}(value) where {V} = new{V}(value)
1112+
Returns(value) = new{Core.Typeof(value)}(value)
1113+
end
1114+
1115+
(obj::Returns)(args...; kw...) = obj.value
1116+
function Base.show(io::IO, obj::Returns)
1117+
show(io, typeof(obj))
1118+
print(io, "(")
1119+
show(io, obj.value)
1120+
print(io, ")")
1121+
end
1122+
end
1123+
11051124
include("iterators.jl")
11061125
include("deprecated.jl")
11071126

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,3 +1144,18 @@ end
11441144
""", sprint(show, excs))
11451145
end
11461146
end
1147+
1148+
# https://github.com/JuliaLang/julia/pull/39794
1149+
@testset "Returns" begin
1150+
@test @inferred(Returns(1)() ) === 1
1151+
@test @inferred(Returns(1)(23) ) === 1
1152+
@test @inferred(Returns("a")(2,3)) == "a"
1153+
@test @inferred(Returns(1)(x=1, y=2)) === 1
1154+
@test @inferred(Returns(Int)()) === Int
1155+
@test @inferred(Returns(Returns(1))()) === Returns(1)
1156+
f = @inferred Returns(Int)
1157+
@inferred f(1,2)
1158+
val = [1,2,3]
1159+
@test Returns(val)(1) === val
1160+
@test sprint(show, Returns(1.0)) == "Returns{Float64}(1.0)"
1161+
end

0 commit comments

Comments
 (0)