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

Port implicit named tuple and keyword argument names #747

Merged
merged 3 commits into from
Jul 28, 2021
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.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