Skip to content

Commit 61726a3

Browse files
authored
Add @something and @coalesce (#742)
1 parent dcfcf4d commit 61726a3

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-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.28.0"
3+
version = "3.29.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+
* `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29)
58+
5759
* `NamedTuple(::Pairs)` ([#37454]) (since Compat 3.28)
5860

5961
* `UUID(::UUID)` and `parse(::Type{UUID}, string)` ([#36018] / [#36199]) (since Compat 3.27)
@@ -242,3 +244,4 @@ Note that you should specify the correct minimum version for `Compat` in the
242244
[#36018]: https://github.com/JuliaLang/julia/pull/36018
243245
[#36199]: https://github.com/JuliaLang/julia/pull/36199
244246
[#37454]: https://github.com/JuliaLang/julia/pull/37454
247+
[#40729]: https://github.com/JuliaLang/julia/pull/40729

src/Compat.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,27 @@ if VERSION < v"1.6.0-DEV.877"
894894
Base.NamedTuple(itr) = (; itr...)
895895
end
896896

897+
# https://github.com/JuliaLang/julia/pull/40729
898+
if VERSION < v"1.7.0-DEV.1088"
899+
macro something(args...)
900+
expr = :(nothing)
901+
for arg in reverse(args)
902+
expr = :((val = $arg) !== nothing ? val : $expr)
903+
end
904+
return esc(:(something(let val; $expr; end)))
905+
end
906+
907+
macro coalesce(args...)
908+
expr = :(missing)
909+
for arg in reverse(args)
910+
expr = :((val = $arg) !== missing ? val : $expr)
911+
end
912+
return esc(:(let val; $expr; end))
913+
end
914+
915+
export @something, @coalesce
916+
end
917+
897918
include("iterators.jl")
898919
include("deprecated.jl")
899920

test/runtests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,3 +884,24 @@ end
884884
f(;kwargs...) = NamedTuple(kwargs)
885885
@test f(a=1, b=2) == (a=1, b=2)
886886
end
887+
888+
# https://github.com/JuliaLang/julia/pull/40729
889+
@testset "@something" begin
890+
@test_throws ArgumentError @something()
891+
@test_throws ArgumentError @something(nothing)
892+
@test @something(1) === 1
893+
@test @something(Some(nothing)) === nothing
894+
895+
@test @something(1, error("failed")) === 1
896+
@test_throws ErrorException @something(nothing, error("failed"))
897+
end
898+
899+
@testset "@coalesce" begin
900+
@test @coalesce() === missing
901+
@test @coalesce(1) === 1
902+
@test @coalesce(nothing) === nothing
903+
@test @coalesce(missing) === missing
904+
905+
@test @coalesce(1, error("failed")) === 1
906+
@test_throws ErrorException @coalesce(missing, error("failed"))
907+
end

0 commit comments

Comments
 (0)