diff --git a/Project.toml b/Project.toml index 125bfdfbd..b06d95a29 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/README.md b/README.md index f7c03486b..701a57004 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/src/compatmacro.jl b/src/compatmacro.jl index 4494a6d3d..bcc663e23 100644 --- a/src/compatmacro.jl +++ b/src/compatmacro.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index f859a1eb1..99b19c6ca 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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()