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

@compat for f.(args...) and foo.:bar #196

Merged
merged 2 commits into from
May 11, 2016
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ wherever you want to use syntax that differs in the latest Julia

Currently, the `@compat` macro supports the following syntaxes:

* `@compat foo.:bar` - `foo.(:bar)` in 0.4 ([#15032]).

* `@compat f.(args...)` - `broadcast(f, args...)` in 0.4 ([#15032]).

* `@compat (a::B{T}){T}(c) = d` - the Julia 0.5-style call overload.

* `@compat Dict(foo => bar, baz => qux)` - type-inferred `Dict` construction. (Also works for `DataStructures.OrderedDict`)
Expand Down Expand Up @@ -204,3 +208,5 @@ bash $ /path/to/Compat/bin/version.sh a378b60fe483130d0d30206deb8ba662e93944da
This prints a version number corresponding to the specified commit of the form
`X.Y.Z-aaa+NNNN`, and you can then test whether Julia
is at least this version by `VERSION >= v"X.Y.Z-aaa+NNNN"`.

[#15032]: https://github.com/JuliaLang/julia/issues/15032
16 changes: 16 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,22 @@ function _compat(ex::Expr)
callexpr.args[2] = params
callexpr.args[3] = obj
end
elseif VERSION < v"0.5.0-dev+4002" && ex.head == :. && length(ex.args) == 2 # 15032
if isexpr(ex.args[2], :quote) && isa(ex.args[2].args[1], QuoteNode)
# foo.:bar -> foo.(:bar) in older Julia
return Expr(ex.head, _compat(ex.args[1]), ex.args[2].args[1])
elseif isexpr(ex.args[2], :quote) && isexpr(ex.args[2].args[1], :quote) &&
isa(ex.args[2].args[1].args[1], Symbol)
# foo.:bar -> foo.(:bar) in older Julia
return Expr(ex.head, _compat(ex.args[1]), QuoteNode(ex.args[2].args[1].args[1]))
elseif isexpr(ex.args[2], :tuple)
# f.(arg1, arg2...) -> broadcast(f, arg1, arg2...)
return Expr(:call, :broadcast, _compat(ex.args[1]), map(_compat, ex.args[2].args)...)
elseif !isa(ex.args[2], QuoteNode) &&
!(isexpr(ex.args[2], :quote) && isa(ex.args[2].args[1], Symbol))
# f.(arg) -> broadcast(f, arg)
return Expr(:call, :broadcast, _compat(ex.args[1]), _compat(ex.args[2]))
end
end
return Expr(ex.head, map(_compat, ex.args)...)
end
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,12 @@ end
@test @compat(Symbol('c')) === :c
@test @compat(Symbol(1)) === @compat(Symbol("1"))

@test @compat(Base.:+) == +
let x = rand(3), y = rand(3)
@test @compat(sin.(cos.(x))) == map(x -> sin(cos(x)), x)
@test @compat(atan2.(sin.(y),x)) == broadcast(atan2,map(sin,y),x)
end

if VERSION ≥ v"0.4.0-dev+3732"
@test Symbol("foo") === :foo
@test Symbol("foo", "bar") === :foobar
Expand Down