Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for property destructuring in @compat macro #768

Merged
merged 6 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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