diff --git a/README.md b/README.md index 1512d5f5c..094b13035 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ changes in `julia`. ## Supported features +* Implicit keywords arguments or named tuples is supported using the `@compat` macro, e.g. `@compat f(; x, f.p)` and `@compat (; x, f.p)` is equivalent to `f(; x=x, p=f.p)` and `(; x=x, f.p)` respectively. ([#34331]) + * `get` accepts tuples and numbers ([#41007], [#41032]) (since Compat 3.31) * `muladd(A,B,z)` now accepts arrays ([#37065]) (since Compat 3.30) @@ -252,3 +254,4 @@ Note that you should specify the correct minimum version for `Compat` in the [#37065]: https://github.com/JuliaLang/julia/pull/37065 [#41007]: https://github.com/JuliaLang/julia/pull/41007 [#41032]: https://github.com/JuliaLang/julia/pull/41032 +[#34331]: https://github.com/JuliaLang/julia/pull/34331 diff --git a/src/compatmacro.jl b/src/compatmacro.jl index 212c7291f..4494a6d3d 100644 --- a/src/compatmacro.jl +++ b/src/compatmacro.jl @@ -8,6 +8,14 @@ function _compat(ex::Expr) # Passthrough return ex end + + # https://github.com/JuliaLang/julia/pull/34331 + @static if VERSION < v"1.5.0-DEV.499" + if ex.head === :call || ex.head === :tuple + ex = _assign_implicit_keywords(ex) + end + end + return Expr(ex.head, map(_compat, ex.args)...) end @@ -66,3 +74,23 @@ function _create_import_expression(path_aliases::Vector{Pair{Vector{Symbol}, Sym return_expr = Expr(:toplevel, module_expr, const_exprs..., nothing) return return_expr end + +function _assign_implicit_keywords(ex::Expr) + kwargs = if ex.head === :call && Meta.isexpr(ex.args[2], :parameters) + ex.args[2].args + elseif ex.head === :tuple && Meta.isexpr(ex.args[1], :parameters) + ex.args[1].args + else + [] + end + + for (i, k) in enumerate(kwargs) + if k isa Symbol + kwargs[i] = Expr(:kw, k, k) + elseif Meta.isexpr(k, :.) + kwargs[i] = Expr(:kw, k.args[2].value, k) + end + end + + return ex +end diff --git a/test/runtests.jl b/test/runtests.jl index 13fe0926f..5d7bb57e8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1067,3 +1067,22 @@ end @test get(() -> c[]+=1, x, (3,2,1)) == 3 end end + +# https://github.com/JuliaLang/julia/pull/34331 +struct X + x +end + +@testset "implicit keywords" begin + f(; x=0) = x + x = 1 + s = X(2) + nested = X(X(3)) + + @test (@compat f(; x)) == 1 + @test (@compat f(; s.x)) == 2 + @test (@compat f(; nested.x.x)) == 3 + @test (@compat (; x)) == (; x=1) + @test (@compat (; s.x)) == (; x=2) + @test (@compat (; nested.x.x)) == (; x=3) +end