Skip to content

Commit

Permalink
Use where syntax in convert function
Browse files Browse the repository at this point in the history
  • Loading branch information
musm committed Apr 17, 2017
1 parent 243d218 commit 8fb718d
Show file tree
Hide file tree
Showing 23 changed files with 92 additions and 92 deletions.
8 changes: 4 additions & 4 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,11 @@ isempty(a::AbstractArray) = (_length(a) == 0)

## Conversions ##

convert{T,N }(::Type{AbstractArray{T,N}}, A::AbstractArray{T,N}) = A
convert{T,S,N}(::Type{AbstractArray{T,N}}, A::AbstractArray{S,N}) = copy!(similar(A,T), A)
convert{T,S,N}(::Type{AbstractArray{T }}, A::AbstractArray{S,N}) = convert(AbstractArray{T,N}, A)
convert(::Type{AbstractArray{T,N}}, A::AbstractArray{T,N}) where {T,N } = A
convert(::Type{AbstractArray{T,N}}, A::AbstractArray{S,N}) where {T,S,N} = copy!(similar(A,T), A)
convert(::Type{AbstractArray{T }}, A::AbstractArray{S,N}) where {T,S,N} = convert(AbstractArray{T,N}, A)

convert{T,N}(::Type{Array}, A::AbstractArray{T,N}) = convert(Array{T,N}, A)
convert(::Type{Array}, A::AbstractArray{T,N}) where {T,N} = convert(Array{T,N}, A)

"""
of_indices(x, y)
Expand Down
12 changes: 6 additions & 6 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ oneunit{T}(x::AbstractMatrix{T}) = _one(oneunit(T), x)

## Conversions ##

convert{T}(::Type{Vector}, x::AbstractVector{T}) = convert(Vector{T}, x)
convert{T}(::Type{Matrix}, x::AbstractMatrix{T}) = convert(Matrix{T}, x)
convert(::Type{Vector}, x::AbstractVector{T}) where {T} = convert(Vector{T}, x)
convert(::Type{Matrix}, x::AbstractMatrix{T}) where {T} = convert(Matrix{T}, x)

convert{T,n}(::Type{Array{T}}, x::Array{T,n}) = x
convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
convert(::Type{Array{T}}, x::Array{T,n}) where {T,n} = x
convert(::Type{Array{T,n}}, x::Array{T,n}) where {T,n} = x

convert{T,n,S}(::Type{Array{T}}, x::AbstractArray{S, n}) = convert(Array{T, n}, x)
convert{T,n,S}(::Type{Array{T,n}}, x::AbstractArray{S,n}) = copy!(Array{T,n}(size(x)), x)
convert(::Type{Array{T}}, x::AbstractArray{S,n}) where {T,n,S} = convert(Array{T,n}, x)
convert(::Type{Array{T,n}}, x::AbstractArray{S,n}) where {T,n,S} = copy!(Array{T,n}(size(x)), x)

promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type(T,S),n}

Expand Down
14 changes: 7 additions & 7 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ end

## Conversions ##

convert{T,N}(::Type{Array{T}}, B::BitArray{N}) = convert(Array{T,N}, B)
convert{T,N}(::Type{Array{T,N}}, B::BitArray{N}) = _convert(Array{T,N}, B) # see #15801
convert(::Type{Array{T}}, B::BitArray{N}) where {T,N} = convert(Array{T,N}, B)
convert(::Type{Array{T,N}}, B::BitArray{N}) where {T,N} = _convert(Array{T,N}, B) # see #15801
function _convert{T,N}(::Type{Array{T,N}}, B::BitArray{N})
A = Array{T}(size(B))
Bc = B.chunks
Expand All @@ -496,8 +496,8 @@ function _convert{T,N}(::Type{Array{T,N}}, B::BitArray{N})
return A
end

convert{T,N}(::Type{BitArray}, A::AbstractArray{T,N}) = convert(BitArray{N}, A)
function convert{T,N}(::Type{BitArray{N}}, A::AbstractArray{T,N})
convert(::Type{BitArray}, A::AbstractArray{T,N}) where {T,N} = convert(BitArray{N}, A)
function convert(::Type{BitArray{N}}, A::AbstractArray{T,N}) where N where T
B = BitArray(size(A))
Bc = B.chunks
l = length(B)
Expand All @@ -522,7 +522,7 @@ function convert{T,N}(::Type{BitArray{N}}, A::AbstractArray{T,N})
return B
end

function convert{N}(::Type{BitArray{N}}, A::Array{Bool,N})
function convert(::Type{BitArray{N}}, A::Array{Bool,N}) where N
B = BitArray(size(A))
Bc = B.chunks
l = length(B)
Expand All @@ -531,8 +531,8 @@ function convert{N}(::Type{BitArray{N}}, A::Array{Bool,N})
return B
end

convert{N}(::Type{BitArray{N}}, B::BitArray{N}) = B
convert{T,N}(::Type{AbstractArray{T,N}}, B::BitArray{N}) = convert(Array{T,N}, B)
convert(::Type{BitArray{N}}, B::BitArray{N}) where {N} = B
convert(::Type{AbstractArray{T,N}}, B::BitArray{N}) where {T,N} = convert(Array{T,N}, B)

reinterpret{N}(::Type{Bool}, B::BitArray, dims::NTuple{N,Int}) = reinterpret(B, dims)
reinterpret{N}(B::BitArray, dims::NTuple{N,Int}) = reshape(B, dims)
Expand Down
2 changes: 1 addition & 1 deletion base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ Task(f::ANY) = ccall(:jl_new_task, Ref{Task}, (Any, Int), f, 0)
# note that there is no actual conversion defined here,
# so the methods and ccall's in Core aren't permitted to use convert
convert(::Type{Any}, x::ANY) = x
convert{T}(::Type{T}, x::T) = x
convert(::Type{T}, x::T) where {T} = x
cconvert{T}(::Type{T}, x) = convert(T, x)
unsafe_convert{T}(::Type{T}, x::T) = x

Expand Down
2 changes: 1 addition & 1 deletion base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
convert(::Type{Char}, x::UInt32) = reinterpret(Char, x)
convert(::Type{Char}, x::Number) = Char(UInt32(x))
convert(::Type{UInt32}, x::Char) = reinterpret(UInt32, x)
convert{T<:Number}(::Type{T}, x::Char) = convert(T, UInt32(x))
convert(::Type{T}, x::Char) where {T<:Number} = convert(T, UInt32(x))

rem{T<:Number}(x::Char, ::Type{T}) = rem(UInt32(x), T)

Expand Down
6 changes: 3 additions & 3 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const Complex128 = Complex{Float64}
const Complex64 = Complex{Float32}
const Complex32 = Complex{Float16}

convert{T<:Real}(::Type{Complex{T}}, x::Real) = Complex{T}(x,0)
convert{T<:Real}(::Type{Complex{T}}, z::Complex) = Complex{T}(real(z),imag(z))
convert{T<:Real}(::Type{T}, z::Complex) =
convert(::Type{Complex{T}}, x::Real) where {T<:Real} = Complex{T}(x,0)
convert(::Type{Complex{T}}, z::Complex) where {T<:Real} = Complex{T}(real(z),imag(z))
convert(::Type{T}, z::Complex) where {T<:Real} =
isreal(z) ? convert(T,real(z)) : throw(InexactError())

convert(::Type{Complex}, z::Complex) = z
Expand Down
12 changes: 6 additions & 6 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1220,22 +1220,22 @@ end
/(r::Use_StepRangeLen_Instead, x::Real) = Use_StepRangeLen_Instead(r.start/x, r.step/x, r.len, r.divisor)
promote_rule{T1,T2}(::Type{Use_StepRangeLen_Instead{T1}},::Type{Use_StepRangeLen_Instead{T2}}) =
Use_StepRangeLen_Instead{promote_type(T1,T2)}
convert{T<:AbstractFloat}(::Type{Use_StepRangeLen_Instead{T}}, r::Use_StepRangeLen_Instead{T}) = r
convert{T<:AbstractFloat}(::Type{Use_StepRangeLen_Instead{T}}, r::Use_StepRangeLen_Instead) =
convert(::Type{Use_StepRangeLen_Instead{T}}, r::Use_StepRangeLen_Instead{T}) where {T<:AbstractFloat} = r
convert(::Type{Use_StepRangeLen_Instead{T}}, r::Use_StepRangeLen_Instead) where {T<:AbstractFloat} =
Use_StepRangeLen_Instead{T}(r.start,r.step,r.len,r.divisor)

promote_rule{F,OR<:OrdinalRange}(::Type{Use_StepRangeLen_Instead{F}}, ::Type{OR}) =
Use_StepRangeLen_Instead{promote_type(F,eltype(OR))}
convert{T<:AbstractFloat}(::Type{Use_StepRangeLen_Instead{T}}, r::OrdinalRange) =
convert(::Type{Use_StepRangeLen_Instead{T}}, r::OrdinalRange) where {T<:AbstractFloat} =
Use_StepRangeLen_Instead{T}(first(r), step(r), length(r), one(T))
convert{T}(::Type{Use_StepRangeLen_Instead}, r::OrdinalRange{T}) =
convert(::Type{Use_StepRangeLen_Instead}, r::OrdinalRange{T}) where {T} =
Use_StepRangeLen_Instead{typeof(float(first(r)))}(first(r), step(r), length(r), one(T))

promote_rule{F,OR<:Use_StepRangeLen_Instead}(::Type{LinSpace{F}}, ::Type{OR}) =
LinSpace{promote_type(F,eltype(OR))}
convert{T<:AbstractFloat}(::Type{LinSpace{T}}, r::Use_StepRangeLen_Instead) =
convert(::Type{LinSpace{T}}, r::Use_StepRangeLen_Instead) where {T<:AbstractFloat} =
linspace(convert(T, first(r)), convert(T, last(r)), convert(T, length(r)))
convert{T<:AbstractFloat}(::Type{LinSpace}, r::Use_StepRangeLen_Instead{T}) =
convert(::Type{LinSpace}, r::Use_StepRangeLen_Instead{T}) where {T<:AbstractFloat} =
convert(LinSpace{T}, r)

reverse(r::Use_StepRangeLen_Instead) = Use_StepRangeLen_Instead(r.start + (r.len-1)*r.step, -r.step, r.len, r.divisor)
Expand Down
4 changes: 2 additions & 2 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ similar{K,V}(d::Dict{K,V}) = Dict{K,V}()
similar{K,V}(d::Dict, ::Type{Pair{K,V}}) = Dict{K,V}()

# conversion between Dict types
function convert{K,V}(::Type{Dict{K,V}},d::Associative)
function convert(::Type{Dict{K,V}},d::Associative) where V where K
h = Dict{K,V}()
for (k,v) in d
ck = convert(K,k)
Expand All @@ -205,7 +205,7 @@ function convert{K,V}(::Type{Dict{K,V}},d::Associative)
end
return h
end
convert{K,V}(::Type{Dict{K,V}},d::Dict{K,V}) = d
convert(::Type{Dict{K,V}},d::Dict{K,V}) where {K,V} = d

hashindex(key, sz) = (((hash(key)%Int) & (sz-1)) + 1)::Int

Expand Down
4 changes: 2 additions & 2 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ macro _propagate_inbounds_meta()
end

convert(::Type{Any}, x::ANY) = x
convert{T}(::Type{T}, x::T) = x
convert(::Type{T}, x::T) where {T} = x

convert(::Type{Tuple{}}, ::Tuple{}) = ()
convert(::Type{Tuple}, x::Tuple) = x
convert{T}(::Type{Tuple{Vararg{T}}}, x::Tuple) = cnvt_all(T, x...)
convert(::Type{Tuple{Vararg{T}}}, x::Tuple) where {T} = cnvt_all(T, x...)
cnvt_all(T) = ()
cnvt_all(T, x, rest...) = tuple(convert(T,x), cnvt_all(T, rest...)...)

Expand Down
4 changes: 2 additions & 2 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ end

rem(x::Integer, ::Type{BigInt}) = convert(BigInt, x)

function convert{T<:Unsigned}(::Type{T}, x::BigInt)
function convert(::Type{T}, x::BigInt) where T<:Unsigned
if sizeof(T) < sizeof(Limb)
convert(T, convert(Limb,x))
else
Expand All @@ -189,7 +189,7 @@ function convert{T<:Unsigned}(::Type{T}, x::BigInt)
end
end

function convert{T<:Signed}(::Type{T}, x::BigInt)
function convert(::Type{T}, x::BigInt) where T<:Signed
n = abs(x.size)
if sizeof(T) < sizeof(Limb)
SLimb = typeof(Signed(one(Limb)))
Expand Down
2 changes: 1 addition & 1 deletion base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ promote_rule{T<:Number}(::Type{<:Irrational}, ::Type{T}) = promote_type(Float64,

convert(::Type{AbstractFloat}, x::Irrational) = Float64(x)
convert(::Type{Float16}, x::Irrational) = Float16(Float32(x))
convert{T<:Real}(::Type{Complex{T}}, x::Irrational) = convert(Complex{T}, convert(T,x))
convert(::Type{Complex{T}}, x::Irrational) where {T<:Real} = convert(Complex{T}, convert(T,x))

@pure function convert{T<:Integer}(::Type{Rational{T}}, x::Irrational)
o = precision(BigFloat)
Expand Down
2 changes: 1 addition & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function convert(::Type{Integer},x::BigFloat)
isinteger(x) || throw(InexactError())
trunc(Integer,x)
end
function convert{T<:Integer}(::Type{T},x::BigFloat)
function convert(::Type{T},x::BigFloat) where T<:Integer
isinteger(x) || throw(InexactError())
trunc(T,x)
end
Expand Down
6 changes: 3 additions & 3 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ module IteratorsMD
CartesianRange{N}(rngs::NTuple{N,Union{Integer,AbstractUnitRange}}) =
CartesianRange(CartesianIndex(map(first, rngs)), CartesianIndex(map(last, rngs)))

convert{N}(::Type{NTuple{N,UnitRange{Int}}}, R::CartesianRange{CartesianIndex{N}}) =
convert(::Type{NTuple{N,UnitRange{Int}}}, R::CartesianRange{CartesianIndex{N}}) where {N} =
map((f,l)->f:l, first(R).I, last(R).I)
convert{N}(::Type{NTuple{N,UnitRange}}, R::CartesianRange) =
convert(::Type{NTuple{N,UnitRange}}, R::CartesianRange) where {N} =
convert(NTuple{N,UnitRange{Int}}, R)
convert{N}(::Type{Tuple{Vararg{UnitRange{Int}}}}, R::CartesianRange{CartesianIndex{N}}) =
convert(::Type{Tuple{Vararg{UnitRange{Int}}}}, R::CartesianRange{CartesianIndex{N}}) where {N} =
convert(NTuple{N,UnitRange{Int}}, R)
convert(::Type{Tuple{Vararg{UnitRange}}}, R::CartesianRange) =
convert(Tuple{Vararg{UnitRange{Int}}}, R)
Expand Down
16 changes: 8 additions & 8 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ Nullable() = Nullable{Union{}}()

eltype{T}(::Type{Nullable{T}}) = T

convert{T}(::Type{Nullable{T}}, x::Nullable{T}) = x
convert( ::Type{Nullable }, x::Nullable ) = x
convert(::Type{Nullable{T}}, x::Nullable{T}) where {T} = x
convert(::Type{Nullable }, x::Nullable ) = x

convert{T}(t::Type{Nullable{T}}, x::Any) = convert(t, convert(T, x))

function convert{T}(::Type{Nullable{T}}, x::Nullable)
function convert(::Type{Nullable{T}}, x::Nullable) where T
return isnull(x) ? Nullable{T}() : Nullable{T}(convert(T, get(x)))
end

convert{T<:Nullable}(::Type{Nullable{T}}, x::T) = Nullable{T}(x)
convert{T}(::Type{Nullable{T}}, x::T) = Nullable{T}(x)
convert{T}(::Type{Nullable }, x::T) = Nullable{T}(x)
convert(::Type{Nullable{T}}, x::T) where {T<:Nullable} = Nullable{T}(x)
convert(::Type{Nullable{T}}, x::T) where {T} = Nullable{T}(x)
convert(::Type{Nullable }, x::T) where {T} = Nullable{T}(x)

convert{T}(::Type{Nullable{T}}, ::Void) = Nullable{T}()
convert( ::Type{Nullable }, ::Void) = Nullable{Union{}}()
convert(::Type{Nullable{T}}, ::Void) where {T} = Nullable{T}()
convert(::Type{Nullable }, ::Void) = Nullable{Union{}}()

promote_rule{S,T}(::Type{Nullable{S}}, ::Type{T}) = Nullable{promote_type(S, T)}
promote_rule{S,T}(::Type{Nullable{S}}, ::Type{Nullable{T}}) = Nullable{promote_type(S, T)}
Expand Down
4 changes: 2 additions & 2 deletions base/pair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ length(p::Pair) = 2
first(p::Pair) = p.first
last(p::Pair) = p.second

convert{A,B}(::Type{Pair{A,B}}, x::Pair{A,B}) = x
function convert{A,B}(::Type{Pair{A,B}}, x::Pair)
convert(::Type{Pair{A,B}}, x::Pair{A,B}) where {A,B} = x
function convert(::Type{Pair{A,B}}, x::Pair) where A where B
Pair{A, B}(convert(A, x[1]), convert(B, x[2]))
end

Expand Down
10 changes: 5 additions & 5 deletions base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const C_NULL = bitcast(Ptr{Void}, 0)

# pointer to integer
convert{T<:Union{Int,UInt}}(::Type{T}, x::Ptr) = bitcast(T, x)
convert{T<:Integer}(::Type{T}, x::Ptr) = convert(T, convert(UInt, x))
convert(::Type{T}, x::Ptr) where {T<:Integer} = convert(T, convert(UInt, x))

# integer to pointer
convert{T}(::Type{Ptr{T}}, x::UInt) = bitcast(Ptr{T}, x)
convert{T}(::Type{Ptr{T}}, x::Int) = bitcast(Ptr{T}, x)
convert(::Type{Ptr{T}}, x::UInt) where {T} = bitcast(Ptr{T}, x)
convert(::Type{Ptr{T}}, x::Int) where {T} = bitcast(Ptr{T}, x)

# pointer to pointer
convert{T}(::Type{Ptr{T}}, p::Ptr{T}) = p
convert{T}(::Type{Ptr{T}}, p::Ptr) = bitcast(Ptr{T}, p)
convert(::Type{Ptr{T}}, p::Ptr{T}) where {T} = p
convert(::Type{Ptr{T}}, p::Ptr) where {T} = bitcast(Ptr{T}, p)

# object to pointer (when used with ccall)
unsafe_convert(::Type{Ptr{UInt8}}, x::Symbol) = ccall(:jl_symbol_name, Ptr{UInt8}, (Any,), x)
Expand Down
34 changes: 17 additions & 17 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -747,52 +747,52 @@ end

promote_rule{T1,T2}(::Type{UnitRange{T1}},::Type{UnitRange{T2}}) =
UnitRange{promote_type(T1,T2)}
convert{T<:Real}(::Type{UnitRange{T}}, r::UnitRange{T}) = r
convert{T<:Real}(::Type{UnitRange{T}}, r::UnitRange) = UnitRange{T}(r.start, r.stop)
convert(::Type{UnitRange{T}}, r::UnitRange{T}) where {T<:Real} = r
convert(::Type{UnitRange{T}}, r::UnitRange) where {T<:Real} = UnitRange{T}(r.start, r.stop)

promote_rule{T1,T2}(::Type{OneTo{T1}},::Type{OneTo{T2}}) =
OneTo{promote_type(T1,T2)}
convert{T<:Real}(::Type{OneTo{T}}, r::OneTo{T}) = r
convert{T<:Real}(::Type{OneTo{T}}, r::OneTo) = OneTo{T}(r.stop)
convert(::Type{OneTo{T}}, r::OneTo{T}) where {T<:Real} = r
convert(::Type{OneTo{T}}, r::OneTo) where {T<:Real} = OneTo{T}(r.stop)

promote_rule{T1,UR<:AbstractUnitRange}(::Type{UnitRange{T1}}, ::Type{UR}) =
UnitRange{promote_type(T1,eltype(UR))}
convert{T<:Real}(::Type{UnitRange{T}}, r::AbstractUnitRange) = UnitRange{T}(first(r), last(r))
convert(::Type{UnitRange{T}}, r::AbstractUnitRange) where {T<:Real} = UnitRange{T}(first(r), last(r))
convert(::Type{UnitRange}, r::AbstractUnitRange) = UnitRange(first(r), last(r))

promote_rule{T1a,T1b,T2a,T2b}(::Type{StepRange{T1a,T1b}},::Type{StepRange{T2a,T2b}}) =
StepRange{promote_type(T1a,T2a),promote_type(T1b,T2b)}
convert{T1,T2}(::Type{StepRange{T1,T2}}, r::StepRange{T1,T2}) = r
convert(::Type{StepRange{T1,T2}}, r::StepRange{T1,T2}) where {T1,T2} = r

promote_rule{T1a,T1b,UR<:AbstractUnitRange}(::Type{StepRange{T1a,T1b}},::Type{UR}) =
StepRange{promote_type(T1a,eltype(UR)),promote_type(T1b,eltype(UR))}
convert{T1,T2}(::Type{StepRange{T1,T2}}, r::Range) =
convert(::Type{StepRange{T1,T2}}, r::Range) where {T1,T2} =
StepRange{T1,T2}(convert(T1, first(r)), convert(T2, step(r)), convert(T1, last(r)))
convert{T}(::Type{StepRange}, r::AbstractUnitRange{T}) =
convert(::Type{StepRange}, r::AbstractUnitRange{T}) where {T} =
StepRange{T,T}(first(r), step(r), last(r))

promote_rule{T1,T2,R1,R2,S1,S2}(::Type{StepRangeLen{T1,R1,S1}},::Type{StepRangeLen{T2,R2,S2}}) =
StepRangeLen{promote_type(T1,T2), promote_type(R1,R2), promote_type(S1,S2)}
convert{T,R,S}(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{T,R,S}) = r
convert{T,R,S}(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen) =
convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{T,R,S}) where {T,R,S} = r
convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen) where {T,R,S} =
StepRangeLen{T,R,S}(convert(R, r.ref), convert(S, r.step), length(r), r.offset)
convert{T}(::Type{StepRangeLen{T}}, r::StepRangeLen) =
convert(::Type{StepRangeLen{T}}, r::StepRangeLen) where {T} =
StepRangeLen(convert(T, r.ref), convert(T, r.step), length(r), r.offset)

promote_rule{T,R,S,OR<:Range}(::Type{StepRangeLen{T,R,S}}, ::Type{OR}) =
StepRangeLen{promote_type(T,eltype(OR)),promote_type(R,eltype(OR)),promote_type(S,eltype(OR))}
convert{T,R,S}(::Type{StepRangeLen{T,R,S}}, r::Range) =
convert(::Type{StepRangeLen{T,R,S}}, r::Range) where {T,R,S} =
StepRangeLen{T,R,S}(R(first(r)), S(step(r)), length(r))
convert{T}(::Type{StepRangeLen{T}}, r::Range) =
convert(::Type{StepRangeLen{T}}, r::Range) where {T} =
StepRangeLen(T(first(r)), T(step(r)), length(r))
convert(::Type{StepRangeLen}, r::Range) = convert(StepRangeLen{eltype(r)}, r)

promote_rule{T1,T2}(::Type{LinSpace{T1}},::Type{LinSpace{T2}}) =
LinSpace{promote_type(T1,T2)}
convert{T}(::Type{LinSpace{T}}, r::LinSpace{T}) = r
convert{T}(::Type{LinSpace{T}}, r::Range) =
convert(::Type{LinSpace{T}}, r::LinSpace{T}) where {T} = r
convert(::Type{LinSpace{T}}, r::Range) where {T} =
LinSpace{T}(first(r), last(r), length(r))
convert{T}(::Type{LinSpace}, r::Range{T}) =
convert(::Type{LinSpace}, r::Range{T}) where {T} =
convert(LinSpace{T}, r)

promote_rule{T,OR<:OrdinalRange}(::Type{LinSpace{T}}, ::Type{OR}) =
Expand All @@ -819,7 +819,7 @@ function vcat{T}(rs::Range{T}...)
return a
end

convert{T}(::Type{Array{T,1}}, r::Range{T}) = vcat(r)
convert(::Type{Array{T,1}}, r::Range{T}) where {T} = vcat(r)
collect(r::Range) = vcat(r)

reverse(r::OrdinalRange) = colon(last(r), -step(r), first(r))
Expand Down
12 changes: 6 additions & 6 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Rational{T<:Integer} <: Real
new(div(num, g), div(den, g))
end
end
Rational(n::T, d::T) where T<:Integer = Rational{T}(n,d)
Rational(n::T, d::T) where {T<:Integer} = Rational{T}(n,d)
Rational(n::Integer, d::Integer) = Rational(promote(n,d)...)
Rational(n::Integer) = Rational(n,one(n))

Expand Down Expand Up @@ -69,23 +69,23 @@ function write(s::IO, z::Rational)
write(s,numerator(z),denominator(z))
end

convert{T<:Integer}(::Type{Rational{T}}, x::Rational) = Rational{T}(convert(T,x.num),convert(T,x.den))
convert{T<:Integer}(::Type{Rational{T}}, x::Integer) = Rational{T}(convert(T,x), convert(T,1))
convert(::Type{Rational{T}}, x::Rational) where {T<:Integer} = Rational{T}(convert(T,x.num),convert(T,x.den))
convert(::Type{Rational{T}}, x::Integer) where {T<:Integer} = Rational{T}(convert(T,x), convert(T,1))

convert(::Type{Rational}, x::Rational) = x
convert(::Type{Rational}, x::Integer) = convert(Rational{typeof(x)},x)

convert(::Type{Bool}, x::Rational) = x==0 ? false : x==1 ? true : throw(InexactError()) # to resolve ambiguity
convert(::Type{Integer}, x::Rational) = (isinteger(x) ? convert(Integer, x.num) : throw(InexactError()))
convert{T<:Integer}(::Type{T}, x::Rational) = (isinteger(x) ? convert(T, x.num) : throw(InexactError()))
convert(::Type{T}, x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num) : throw(InexactError()))

convert(::Type{AbstractFloat}, x::Rational) = float(x.num)/float(x.den)
function convert{T<:AbstractFloat,S}(::Type{T}, x::Rational{S})
function convert(::Type{T}, x::Rational{S}) where T<:AbstractFloat where S
P = promote_type(T,S)
convert(T, convert(P,x.num)/convert(P,x.den))
end

function convert{T<:Integer}(::Type{Rational{T}}, x::AbstractFloat)
function convert(::Type{Rational{T}}, x::AbstractFloat) where T<:Integer
r = rationalize(T, x, tol=0)
x == convert(typeof(x), r) || throw(InexactError())
r
Expand Down
Loading

0 comments on commit 8fb718d

Please sign in to comment.