Skip to content

Commit

Permalink
Port implicit named tuple and keyword argument names (#747)
Browse files Browse the repository at this point in the history
* Port implicit named tuple and keyword argument names

* Set version to 3.32.0
  • Loading branch information
omus authored Jul 28, 2021
1 parent aae32f1 commit 28ac4ef
Show file tree
Hide file tree
Showing 4 changed files with 51 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.31.1"
version = "3.32.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

* 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, p=f.p)` respectively. ([#34331]) (since Compat 3.32.0)

* Two argument methods `findmax(f, domain)`, `argmax(f, domain)` and the corresponding `min` versions ([#35316], [#41076]) (since Compat 3.31.1)

* `isunordered(x)` returns true if `x` is value that is normally unordered, such as `NaN` or `missing` ([#35316]) (since Compat 3.31.1)
Expand Down Expand Up @@ -258,3 +260,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#41032]: https://github.com/JuliaLang/julia/pull/41032
[#35316]: https://github.com/JuliaLang/julia/pull/35316
[#41076]: https://github.com/JuliaLang/julia/pull/41076
[#34331]: https://github.com/JuliaLang/julia/pull/34331
28 changes: 28 additions & 0 deletions src/compatmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,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

2 comments on commit 28ac4ef

@omus
Copy link
Member Author

@omus omus commented on 28ac4ef Jul 28, 2021

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

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.32.0 -m "<description of version>" 28ac4ef423b2551753a3c908a4822af69ce5487f
git push origin v3.32.0

Please sign in to comment.