Skip to content

Commit

Permalink
Compat for at-vectorize_(1|2)arg deprecation
Browse files Browse the repository at this point in the history
Provide `Compat.@vectorize_1arg` and `Compat.@vectorize_2arg` as an upgrade
path for packages.
  • Loading branch information
yuyichao committed Sep 5, 2016
1 parent dcfc172 commit 9cfc752
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Compat.@blasfunc` makes functionality of `Base.LinAlg.BLAS.@blasfunc` available on older Julia versions

* `@vectorize_1arg` and `@vectorize_2arg` are deprecated on Julia 0.6 in favor
of the broadcast syntax. `Compat.@vectorize_1arg` and `Compat.@vectorize_2arg`
are provided so that packages can still provide the deprecated definitions
without causing a depwarn in the package itself before all the users
are upgraded.

## Other changes

* `Dict(ks, vs)` is now `Dict(zip(ks, vs))` [#8521](https://github.com/JuliaLang/julia/pull/8521)
Expand Down
31 changes: 31 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1463,4 +1463,35 @@ if VERSION < v"0.6.0-dev.374"
end
end

# PR #17302
# Provide a non-deprecated version of `@vectorize_(1|2)arg` macro which defines
# deprecated version of the function so that the depwarns can be fixed without
# breaking users.
if VERSION >= v"0.6.0-dev.481"
# Copied from Base.
macro vectorize_1arg(S, f)
S = esc(S)
f = esc(f)
T = esc(:T)
x = esc(:x)
AbsArr = esc(:AbstractArray)
:(@deprecate $f{$T<:$S}($x::$AbsArr{$T}) $f.($x))
end

macro vectorize_2arg(S, f)
S = esc(S)
f = esc(f)
T1 = esc(:T1)
T2 = esc(:T2)
x = esc(:x)
y = esc(:y)
AbsArr = esc(:AbstractArray)
quote
@deprecate $f{$T1<:$S}($x::$S, $y::$AbsArr{$T1}) $f.($x,$y)
@deprecate $f{$T1<:$S}($x::$AbsArr{$T1}, $y::$S) $f.($x,$y)
@deprecate $f{$T1<:$S,$T2<:$S}($x::$AbsArr{$T1}, $y::$AbsArr{$T2}) $f.($x,$y)
end
end
end

end # module
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1381,3 +1381,19 @@ let filename = tempname()
@test contains(ret, "WARNING: hello")
rm(filename)
end

# PR #17302
f17302(a::Number) = a
f17302(a::Number, b::Number) = a + b
Compat.@vectorize_1arg Real f17302
Compat.@vectorize_2arg Real f17302
@test_throws MethodError f17302([1im])
@test_throws MethodError f17302([1im], [1im])
mktemp() do fname, f
redirect_stderr(f) do
@test f17302([1.0]) == [1.0]
@test f17302(1.0, [1]) == [2.0]
@test f17302([1.0], 1) == [2.0]
@test f17302([1.0], [1]) == [2.0]
end
end

0 comments on commit 9cfc752

Please sign in to comment.