From 5039cb1011bb385de4ab4bd4a2144671dd5e5804 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 16 Oct 2014 10:15:34 -0400 Subject: [PATCH] deprecate `oftype(type,x)`, which is identical to `convert` --- base/complex.jl | 2 +- base/deprecated.jl | 2 ++ base/floatfuncs.jl | 2 +- base/linalg/dense.jl | 2 +- base/number.jl | 4 ++-- base/operators.jl | 3 +-- base/range.jl | 22 +++++++++++----------- doc/manual/performance-tips.rst | 3 +-- doc/stdlib/base.rst | 2 +- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/base/complex.jl b/base/complex.jl index 50957e4d5fc56..58fa5dc87277c 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -286,7 +286,7 @@ angle(z::Complex) = atan2(imag(z), real(z)) function log{T<:FloatingPoint}(z::Complex{T}) const T1::T = 1.25 const T2::T = 3 - const ln2::T = log(oftype(T,2)) #0.6931471805599453 + const ln2::T = log(convert(T,2)) #0.6931471805599453 x, y = reim(z) ρ, k = ssqs(x,y) ax = abs(x) diff --git a/base/deprecated.jl b/base/deprecated.jl index a4714f9d82687..bde547210bd5d 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -195,3 +195,5 @@ const None = Union() @deprecate Dict{K}(ks::(K...), vs::Tuple) Dict{K,Any}(zip(ks, vs)) @deprecate Dict{V}(ks::Tuple, vs::(V...)) Dict{Any,V}(zip(ks, vs)) @deprecate Dict(ks, vs) Dict{Any,Any}(zip(ks, vs)) + +@deprecate oftype{T}(::Type{T},c) convert(T,c) diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 199bebdefcd76..06cedbac362c6 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -83,7 +83,7 @@ for f in (:round, :ceil, :floor, :trunc) @eval begin function ($f)(x, digits::Integer, base::Integer=10) x = float(x) - og = oftype(eltype(x),base)^digits + og = convert(eltype(x),base)^digits ($f)(x * og) / og end end diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index 0960c65468f3a..d593d54bc2cc5 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -246,7 +246,7 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T}) s = log2(nA/5.4) # power of 2 later reversed by squaring if s > 0 si = iceil(s) - A /= oftype(T,2^si) + A /= convert(T,2^si) end CC = T[64764752532480000.,32382376266240000.,7771770303897600., 1187353796428800., 129060195264000., 10559470521600., diff --git a/base/number.jl b/base/number.jl index 68d5f0628e53d..a19f9179a60c3 100644 --- a/base/number.jl +++ b/base/number.jl @@ -44,9 +44,9 @@ reinterpret{T,S}(::Type{T}, x::S) = box(T,unbox(S,x)) map(f::Callable, x::Number) = f(x) zero(x::Number) = oftype(x,0) -zero{T<:Number}(::Type{T}) = oftype(T,0) +zero{T<:Number}(::Type{T}) = convert(T,0) one(x::Number) = oftype(x,1) -one{T<:Number}(::Type{T}) = oftype(T,1) +one{T<:Number}(::Type{T}) = convert(T,1) const _numeric_conversion_func_names = (:int,:integer,:signed,:int8,:int16,:int32,:int64,:int128, diff --git a/base/operators.jl b/base/operators.jl index ded6dfd9e42ed..003fee43f51e1 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -163,8 +163,7 @@ A_ldiv_Bt (a,b) = a\transpose(b) At_ldiv_Bt(a,b) = transpose(a)\transpose(b) -oftype{T}(::Type{T},c) = convert(T,c) -oftype{T}(x::T,c) = convert(T,c) +oftype(x,c) = convert(typeof(x),c) widen{T<:Number}(x::T) = convert(widen(T), x) diff --git a/base/range.jl b/base/range.jl index da7b0b9e25e88..1e57ea462d9ed 100644 --- a/base/range.jl +++ b/base/range.jl @@ -39,9 +39,9 @@ immutable StepRange{T,S} <: OrdinalRange{T,S} if T<:Signed && (diff > zero(diff)) != (stop > start) # handle overflowed subtraction with unsigned rem if diff > zero(diff) - remain = -oftype(T, unsigned(-diff) % step) + remain = -convert(T, unsigned(-diff) % step) else - remain = oftype(T, unsigned(diff) % step) + remain = convert(T, unsigned(diff) % step) end else remain = steprem(start,stop,step) @@ -188,9 +188,9 @@ length(r::FloatRange) = integer(r.len) function length{T<:Union(Int,Uint,Int64,Uint64)}(r::StepRange{T}) isempty(r) && return zero(T) if r.step > 1 - return checked_add(oftype(T, div(unsigned(r.stop - r.start), r.step)), one(T)) + return checked_add(convert(T, div(unsigned(r.stop - r.start), r.step)), one(T)) elseif r.step < -1 - return checked_add(oftype(T, div(unsigned(r.start - r.stop), -r.step)), one(T)) + return checked_add(convert(T, div(unsigned(r.start - r.stop), -r.step)), one(T)) else checked_add(div(checked_sub(r.stop, r.start), r.step), one(T)) end @@ -213,12 +213,12 @@ let smallint = (Int === Int64 ? length{T <: smallint}(r::UnitRange{T}) = int(r.stop) - int(r.start) + 1 end -first{T}(r::OrdinalRange{T}) = oftype(T, r.start) +first{T}(r::OrdinalRange{T}) = convert(T, r.start) first(r::FloatRange) = r.start/r.divisor last{T}(r::StepRange{T}) = r.stop last(r::UnitRange) = r.stop -last{T}(r::FloatRange{T}) = oftype(T, (r.start + (r.len-1)*r.step)/r.divisor) +last{T}(r::FloatRange{T}) = convert(T, (r.start + (r.len-1)*r.step)/r.divisor) minimum(r::UnitRange) = isempty(r) ? error("range must be non-empty") : first(r) maximum(r::UnitRange) = isempty(r) ? error("range must be non-empty") : last(r) @@ -235,18 +235,18 @@ copy(r::Range) = r ## iteration start(r::FloatRange) = 0 -next{T}(r::FloatRange{T}, i) = (oftype(T, (r.start + i*r.step)/r.divisor), i+1) +next{T}(r::FloatRange{T}, i) = (convert(T, (r.start + i*r.step)/r.divisor), i+1) done(r::FloatRange, i) = (length(r) <= i) # NOTE: For ordinal ranges, we assume start+step might be from a # lifted domain (e.g. Int8+Int8 => Int); use that for iterating. start(r::StepRange) = convert(typeof(r.start+r.step), r.start) -next{T}(r::StepRange{T}, i) = (oftype(T,i), i+r.step) +next{T}(r::StepRange{T}, i) = (convert(T,i), i+r.step) done{T,S}(r::StepRange{T,S}, i) = isempty(r) | (i < min(r.start, r.stop)) | (i > max(r.start, r.stop)) done{T,S}(r::StepRange{T,S}, i::Integer) = isempty(r) | (i == r.stop+r.step) start(r::UnitRange) = oftype(r.start+1, r.start) -next{T}(r::UnitRange{T}, i) = (oftype(T,i), i+1) +next{T}(r::UnitRange{T}, i) = (convert(T,i), i+1) done(r::UnitRange, i) = i==oftype(i,r.stop)+1 @@ -256,11 +256,11 @@ getindex(r::Range, i::Real) = getindex(r, to_index(i)) function getindex{T}(r::Range{T}, i::Integer) 1 <= i <= length(r) || error(BoundsError) - oftype(T, first(r) + (i-1)*step(r)) + convert(T, first(r) + (i-1)*step(r)) end function getindex{T}(r::FloatRange{T}, i::Integer) 1 <= i <= length(r) || error(BoundsError) - oftype(T, (r.start + (i-1)*r.step)/r.divisor) + convert(T, (r.start + (i-1)*r.step)/r.divisor) end function check_indexingrange(s, r) diff --git a/doc/manual/performance-tips.rst b/doc/manual/performance-tips.rst index 459927f6a8a91..1e000bd7926cf 100644 --- a/doc/manual/performance-tips.rst +++ b/doc/manual/performance-tips.rst @@ -254,8 +254,7 @@ some cases. But it can easily be fixed as follows:: pos(x) = x < 0 ? zero(x) : x There is also a ``one`` function, and a more general ``oftype(x,y)`` -function, which returns ``y`` converted to the type of ``x``. The first -argument to any of these functions can be either a value or a type. +function, which returns ``y`` converted to the type of ``x``. Avoid changing the type of a variable ------------------------------------- diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 5212de6c05681..b9ff934313fc2 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -243,7 +243,7 @@ All Objects .. function:: oftype(x, y) - Convert ``y`` to the type of ``x``. + Convert ``y`` to the type of ``x`` (``convert(typeof(x), y)``). .. function:: widen(type | x)