Skip to content

Commit

Permalink
Add @something and @coalesce (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed May 14, 2021
1 parent dcfcf4d commit 61726a3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.28.0"
version = "3.29.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ changes in `julia`.

## Supported features

* `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29)

* `NamedTuple(::Pairs)` ([#37454]) (since Compat 3.28)

* `UUID(::UUID)` and `parse(::Type{UUID}, string)` ([#36018] / [#36199]) (since Compat 3.27)
Expand Down Expand Up @@ -242,3 +244,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#36018]: https://github.com/JuliaLang/julia/pull/36018
[#36199]: https://github.com/JuliaLang/julia/pull/36199
[#37454]: https://github.com/JuliaLang/julia/pull/37454
[#40729]: https://github.com/JuliaLang/julia/pull/40729
21 changes: 21 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,27 @@ if VERSION < v"1.6.0-DEV.877"
Base.NamedTuple(itr) = (; itr...)
end

# https://github.com/JuliaLang/julia/pull/40729
if VERSION < v"1.7.0-DEV.1088"
macro something(args...)
expr = :(nothing)
for arg in reverse(args)
expr = :((val = $arg) !== nothing ? val : $expr)
end
return esc(:(something(let val; $expr; end)))
end

macro coalesce(args...)
expr = :(missing)
for arg in reverse(args)
expr = :((val = $arg) !== missing ? val : $expr)
end
return esc(:(let val; $expr; end))
end

export @something, @coalesce
end

include("iterators.jl")
include("deprecated.jl")

Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,24 @@ end
f(;kwargs...) = NamedTuple(kwargs)
@test f(a=1, b=2) == (a=1, b=2)
end

# https://github.com/JuliaLang/julia/pull/40729
@testset "@something" begin
@test_throws ArgumentError @something()
@test_throws ArgumentError @something(nothing)
@test @something(1) === 1
@test @something(Some(nothing)) === nothing

@test @something(1, error("failed")) === 1
@test_throws ErrorException @something(nothing, error("failed"))
end

@testset "@coalesce" begin
@test @coalesce() === missing
@test @coalesce(1) === 1
@test @coalesce(nothing) === nothing
@test @coalesce(missing) === missing

@test @coalesce(1, error("failed")) === 1
@test_throws ErrorException @coalesce(missing, error("failed"))
end

2 comments on commit 61726a3

@omus
Copy link
Member Author

@omus omus commented on 61726a3 May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/36757

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.29.0 -m "<description of version>" 61726a3b3cc9c6a3ac176263a81dd06d176bf500
git push origin v3.29.0

Please sign in to comment.