Skip to content

Commit 03fd3eb

Browse files
committed
@compat for f.(args...) and foo.:bar
1 parent 5a9c2a5 commit 03fd3eb

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ wherever you want to use syntax that differs in the latest Julia
3636

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

39+
* `@compat foo.:bar` - `foo.(:bar)` in 0.4 ([#15032]).
40+
41+
* `@compat f.(args...)` - `broadcast(f, args...)` in 0.4 ([#15032]).
42+
3943
* `@compat (a::B{T}){T}(c) = d` - the Julia 0.5-style call overload.
4044

4145
* `@compat Dict(foo => bar, baz => qux)` - type-inferred `Dict` construction. (Also works for `DataStructures.OrderedDict`)
@@ -204,3 +208,5 @@ bash $ /path/to/Compat/bin/version.sh a378b60fe483130d0d30206deb8ba662e93944da
204208
This prints a version number corresponding to the specified commit of the form
205209
`X.Y.Z-aaa+NNNN`, and you can then test whether Julia
206210
is at least this version by `VERSION >= v"X.Y.Z-aaa+NNNN"`.
211+
212+
[#15032]: https://github.com/JuliaLang/julia/issues/15032

src/Compat.jl

+16
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,22 @@ function _compat(ex::Expr)
541541
callexpr.args[2] = params
542542
callexpr.args[3] = obj
543543
end
544+
elseif VERSION < v"0.5.0-dev+4002" && ex.head == :. && length(ex.args) == 2 # 15032
545+
if isexpr(ex.args[2], :quote) && typeof(ex.args[2].args[1]) == QuoteNode
546+
# foo.:bar -> foo.(:bar) in older Julia
547+
return Expr(ex.head, _compat(ex.args[1]), ex.args[2].args[1])
548+
elseif isexpr(ex.args[2], :quote) && isexpr(ex.args[2].args[1], :quote) &&
549+
typeof(ex.args[2].args[1].args[1]) == Symbol
550+
# foo.:bar -> foo.(:bar) in older Julia
551+
return Expr(ex.head, _compat(ex.args[1]), QuoteNode(ex.args[2].args[1].args[1]))
552+
elseif isexpr(ex.args[2], :tuple)
553+
# f.(arg1, arg2...) -> broadcast(f, arg1, arg2...)
554+
return Expr(:call, :broadcast, _compat(ex.args[1]), map(_compat, ex.args[2].args)...)
555+
elseif typeof(ex.args[2]) != QuoteNode &&
556+
!(isexpr(ex.args[2], :quote) && typeof(ex.args[2].args[1]) == Symbol)
557+
# f.(arg) -> broadcast(f, arg)
558+
return Expr(:call, :broadcast, _compat(ex.args[1]), _compat(ex.args[2]))
559+
end
544560
end
545561
return Expr(ex.head, map(_compat, ex.args)...)
546562
end

test/runtests.jl

+6
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,12 @@ end
11091109
@test @compat(Symbol('c')) === :c
11101110
@test @compat(Symbol(1)) === @compat(Symbol("1"))
11111111

1112+
@test @compat(Base.:+) == +
1113+
let x = rand(3), y = rand(3)
1114+
@test @compat(sin.(cos.(x))) == map(x -> sin(cos(x)), x)
1115+
@test @compat(atan2.(sin.(y),x)) == broadcast(atan2,map(sin,y),x)
1116+
end
1117+
11121118
if VERSION v"0.4.0-dev+3732"
11131119
@test Symbol("foo") === :foo
11141120
@test Symbol("foo", "bar") === :foobar

0 commit comments

Comments
 (0)