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 3 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
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
19 changes: 19 additions & 0 deletions src/compatmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function _compat(ex::Expr)
end
end

@static if VERSION < v"1.7.0"
kleinschmidt marked this conversation as resolved.
Show resolved Hide resolved
ex = _destructure_named_tuple(ex)
end

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

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

return ex
end

function _destructure_named_tuple(ex::Expr)
if ex.head === :(=) && ex.args[1] isa Expr && ex.args[1].head === :tuple &&
ex.args[1].args[1] isa Expr && ex.args[1].args[1].head === :parameters
ararslan marked this conversation as resolved.
Show resolved Hide resolved
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)
end
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