Skip to content

Commit

Permalink
Add support for property destructuring in @compat macro (#768)
Browse files Browse the repository at this point in the history
* add property destructuring assignment to `@compat`

* update readme

* one more test

* Update src/compatmacro.jl

Co-authored-by: Alex Arslan <ararslan@comcast.net>

* hoist guard adn use Meta.isexpr

* bump minor version (new feature)

Co-authored-by: Alex Arslan <ararslan@comcast.net>
  • Loading branch information
kleinschmidt and ararslan authored Apr 11, 2022
1 parent fa274f7 commit 3ae185f
Show file tree
Hide file tree
Showing 4 changed files with 38 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.42.0"
version = "3.43.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

* `@compat (; a, b) = (; c=1, b=2, a=3)` supports property descturing assignment syntax ([#39285]).

* `allequal`, the opposite of `allunique` ([#43354]). (since Compat 3.42.0)

* `eachsplit` for iteratively performing split(str). ([#39245]). (since Compat 3.41.0)
Expand Down Expand Up @@ -293,3 +295,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#41312]: https://github.com/JuliaLang/julia/pull/41312
[#41328]: https://github.com/JuliaLang/julia/pull/41328
[#43354]: https://github.com/JuliaLang/julia/pull/43354
[#39285]: https://github.com/JuliaLang/julia/pull/39285
22 changes: 22 additions & 0 deletions src/compatmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ function _compat(ex::Expr)
end
end

# https://github.com/JuliaLang/julia/pull/39285
@static if VERSION < v"1.7.0-DEV.364"
if Meta.isexpr(ex, :(=)) && Meta.isexpr(ex.args[1], :tuple) &&
Meta.isexpr(ex.args[1].args[1], :parameters)

ex = _destructure_named_tuple(ex)
end
end

return Expr(ex.head, map(_compat, ex.args)...)
end

Expand Down Expand Up @@ -94,3 +103,16 @@ function _assign_implicit_keywords(ex::Expr)

return ex
end

function _destructure_named_tuple(ex::Expr)
ex.args[1].args[1] isa Expr && ex.args[1].args[1].head === :parameters
values = ex.args[2]
parameters = ex.args[1].args[1].args
ex = Expr(:block)
for p in parameters
asgn = Expr(:(=), p, Expr(:., values, QuoteNode(p)))
push!(ex.args, asgn)
end
push!(ex.args, values)
return ex
end
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,18 @@ end
@test (@compat (; nested.x.x)) == (; x=3)
end

# https://github.com/JuliaLang/julia/pull/39285
@testset "property destructuring assignment" begin
nt = (; a=1, b=2, c=3)
@compat (; c, b) = nt
@test c == nt.c
@test b == nt.b

@compat (; x) = X(1)
@test x == 1
end


# https://github.com/JuliaLang/julia/pull/34595
@testset "include(mapexpr::Function, ...)" begin
m = Module()
Expand Down

2 comments on commit 3ae185f

@ararslan
Copy link
Member

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/58370

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.43.0 -m "<description of version>" 3ae185fa45a3091b387b67b6fd8f58f761854238
git push origin v3.43.0

Please sign in to comment.