Skip to content

Commit

Permalink
Cleanup Julia 0.7 deprecations (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
omus authored and ararslan committed Nov 29, 2017
1 parent c72b467 commit 47a5a7a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 63 deletions.
66 changes: 34 additions & 32 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,36 @@ end

export @compat

# https://github.com/JuliaLang/julia/pull/22064
@static if !isdefined(Base, Symbol("@__MODULE__"))
# 0.7
export @__MODULE__
macro __MODULE__()
return current_module()
end
Base.expand(mod::Module, x::ANY) = eval(mod, :(expand($(QuoteNode(x)))))
Base.macroexpand(mod::Module, x::ANY) = eval(mod, :(macroexpand($(QuoteNode(x)))))
Base.include_string(mod::Module, code::String, fname::String) =
eval(mod, :(include_string($code, $fname)))
Base.include_string(mod::Module, code::AbstractString, fname::AbstractString="string") =
eval(mod, :(include_string($code, $fname)))
end

if VERSION < v"0.6.0-dev.2042"
immutable ExponentialBackOff
n::Int
first_delay::Float64
max_delay::Float64
factor::Float64
jitter::Float64

function ExponentialBackOff(n, first_delay, max_delay, factor, jitter)
all(x->x>=0, (n, first_delay, max_delay, factor, jitter)) || error("all inputs must be non-negative")
new(n, first_delay, max_delay, factor, jitter)
include_string(@__MODULE__, """
immutable ExponentialBackOff
n::Int
first_delay::Float64
max_delay::Float64
factor::Float64
jitter::Float64
function ExponentialBackOff(n, first_delay, max_delay, factor, jitter)
all(x->x>=0, (n, first_delay, max_delay, factor, jitter)) || error("all inputs must be non-negative")
new(n, first_delay, max_delay, factor, jitter)
end
end
end
""")

"""
ExponentialBackOff(; n=1, first_delay=0.05, max_delay=10.0, factor=5.0, jitter=0.1)
Expand Down Expand Up @@ -287,7 +304,7 @@ else
end

# broadcast over same length tuples, from julia#16986
if VERSION < v"0.6.0-dev.693"
@static if VERSION < v"0.6.0-dev.693"
Base.Broadcast.broadcast{N}(f, t::NTuple{N}, ts::Vararg{NTuple{N}}) = map(f, t, ts...)
end

Expand Down Expand Up @@ -440,7 +457,7 @@ end
end

# https://github.com/JuliaLang/julia/pull/18727
if VERSION < v"0.6.0-dev.838"
@static if VERSION < v"0.6.0-dev.838"
Base.convert{T}(::Type{Set{T}}, s::Set{T}) = s
Base.convert{T}(::Type{Set{T}}, s::Set) = Set{T}(s)
end
Expand All @@ -450,7 +467,7 @@ if VERSION < v"0.6.0-dev.2347"
Base.isassigned(x::Base.RefValue) = isdefined(x, :x)
end

if VERSION < v"0.6.0-dev.735"
@static if VERSION < v"0.6.0-dev.735"
Base.unsafe_trunc{T<:Integer}(::Type{T}, x::Integer) = rem(x, T)
end

Expand All @@ -466,21 +483,6 @@ else
using Base: StringVector
end

# https://github.com/JuliaLang/julia/pull/22064
@static if !isdefined(Base, Symbol("@__MODULE__"))
# 0.7
export @__MODULE__
macro __MODULE__()
return current_module()
end
Base.expand(mod::Module, x::ANY) = eval(mod, :(expand($(QuoteNode(x)))))
Base.macroexpand(mod::Module, x::ANY) = eval(mod, :(macroexpand($(QuoteNode(x)))))
Base.include_string(mod::Module, code::String, fname::String) =
eval(mod, :(include_string($code, $fname)))
Base.include_string(mod::Module, code::AbstractString, fname::AbstractString="string") =
eval(mod, :(include_string($code, $fname)))
end

# https://github.com/JuliaLang/julia/pull/19784
@static if isdefined(Base, :invokelatest)
# 0.6
Expand Down Expand Up @@ -529,7 +531,7 @@ if VERSION < v"0.6.0-pre.beta.455"
end

# https://github.com/JuliaLang/julia/pull/22475
if VERSION < v"0.7.0-DEV.843"
@static if VERSION < v"0.7.0-DEV.843"
import Base: Val
(::Type{Val})(x) = (Base.@_pure_meta; Val{x}())
# Also add methods for Val(x) that were previously Val{x}
Expand Down Expand Up @@ -769,7 +771,7 @@ end
end
end

if VERSION < v"0.7.0-DEV.2377"
@static if VERSION < v"0.7.0-DEV.2377"
(::Type{Matrix{T}}){T}(s::UniformScaling, dims::Dims{2}) = setindex!(zeros(T, dims), T(s.λ), diagind(dims...))
(::Type{Matrix{T}}){T}(s::UniformScaling, m::Integer, n::Integer) = Matrix{T}(s, Dims((m, n)))

Expand All @@ -788,7 +790,7 @@ if VERSION < v"0.7.0-DEV.2377"
SparseMatrixCSC{Tv,Ti}(dims..., colptr, rowval, nzval)
end
end
if VERSION < v"0.7.0-DEV.2543"
@static if VERSION < v"0.7.0-DEV.2543"
(::Type{Array{T}}){T}(s::UniformScaling, dims::Dims{2}) = Matrix{T}(s, dims)
(::Type{Array{T}}){T}(s::UniformScaling, m::Integer, n::Integer) = Matrix{T}(s, m, n)
end
Expand Down
67 changes: 36 additions & 31 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Compat
using Compat.Test

const struct_sym = VERSION < v"0.7.0-DEV.1263" ? :type : :struct

# Issue #291
# 0.6
@test (1, 2) == @compat abs.((1, -2))
Expand Down Expand Up @@ -338,13 +340,15 @@ end
@test !iszero([0, 1, 2, 3])
@test iszero([0, 0, 0, 0])

x = view(1:10, 2:4)
D = Diagonal(x)
@test D[1,1] == 2
@test D[3,3] == 4
A = view(rand(5,5), 1:3, 1:3)
@test D*A == Diagonal(copy(x)) * copy(A)
@test A*D == copy(A) * Diagonal(copy(x))
let
x = view(1:10, 2:4)
D = Diagonal(x)
@test D[1,1] == 2
@test D[3,3] == 4
A = view(rand(5,5), 1:3, 1:3)
@test D*A == Diagonal(copy(x)) * copy(A)
@test A*D == copy(A) * Diagonal(copy(x))
end

# julia#17623
# 0.6
Expand All @@ -365,11 +369,11 @@ end
# julia#20006
@compat abstract type AbstractFoo20006 end
eval(Expr(
:type, false,
struct_sym, false,
Expr(:(<:), :(ConcreteFoo20006{T<:Int}), :AbstractFoo20006),
quote end))
eval(Expr(
:type, false,
struct_sym, false,
Expr(:(<:), :(ConcreteFoo20006N{T<:Int,N}), :AbstractFoo20006),
quote end))
@compat ConcreteFoo200061{T<:Int} = ConcreteFoo20006N{T,1}
Expand Down Expand Up @@ -541,20 +545,21 @@ f20500_2() = A20500_2
@inferred f20500_2()

module CompatArray
using Compat
eval(Expr(
:type, false,
Expr(:(<:), :(CartesianArray{T,N}), :(AbstractArray{T,N})),
quote
parent::Array{T,N}
end))
eval(Expr(
:type, false,
Expr(:(<:), :(LinearArray{T,N}), :(AbstractArray{T,N})),
quote
parent::Array{T,N}
end))
@compat Base.IndexStyle(::Type{<:LinearArray}) = IndexLinear()
using Compat
const struct_sym = VERSION < v"0.7.0-DEV.1263" ? :type : :struct
eval(Expr(
struct_sym, false,
Expr(:(<:), :(CartesianArray{T,N}), :(AbstractArray{T,N})),
quote
parent::Array{T,N}
end))
eval(Expr(
struct_sym, false,
Expr(:(<:), :(LinearArray{T,N}), :(AbstractArray{T,N})),
quote
parent::Array{T,N}
end))
@compat Base.IndexStyle(::Type{<:LinearArray}) = IndexLinear()
end
@test IndexStyle(Array{Float32,2}) === IndexLinear()
@test IndexStyle(CompatArray.CartesianArray{Float32,2}) === IndexCartesian()
Expand All @@ -564,14 +569,14 @@ let a = CompatArray.CartesianArray(rand(2,3)), b = CompatArray.LinearArray(rand(
@test IndexStyle(b) === IndexLinear()
end

for (A,val) in ((zeros(1:5, Float32, 3, 2), 0),
(ones(1:5, Float32, 3, 2), 1),
(zeros(1:5, Float32, (3, 2)), 0),
(ones(1:5, Float32, (3, 2)), 1))
for (A,val) in ((fill!(Array{Float32}(3, 2), 0), 0),
(fill!(Array{Float32}(3, 2), 1), 1),
(fill!(Array{Float32}(3, 2), 0), 0),
(fill!(Array{Float32}(3, 2), 1), 1))
@test isa(A, Matrix{Float32}) && size(A) == (3,2) && all(x->x==val, A)
end
for (A,val) in ((zeros(1:5, Float32), 0),
(ones(1:5, Float32), 1))
for (A,val) in ((fill!(Array{Float32}(5), 0), 0),
(fill!(Array{Float32}(5), 1), 1))
@test isa(A, Vector{Float32}) && size(A) == (5,) && all(x->x==val, A)
end

Expand Down Expand Up @@ -649,7 +654,7 @@ let

# https://en.wikipedia.org/wiki/Swatch_Internet_Time
eval(Expr(
:type, false,
struct_sym, false,
Expr(:(<:), :Beat, :(Dates.Period)),
quote
value::Int64
Expand Down Expand Up @@ -705,7 +710,7 @@ if VERSION < v"0.7.0-DEV.880"
end

# PR 22350
eval(Expr(:type, false, :TestType, Expr(:block, :(a::Int), :b)))
eval(Expr(struct_sym, false, :TestType, Expr(:block, :(a::Int), :b)))
@test fieldcount(TestType) == 2
@test fieldcount(Int) == 0

Expand Down

0 comments on commit 47a5a7a

Please sign in to comment.