diff --git a/base/Enums.jl b/base/Enums.jl index a0ce5dc5692a33..c597b0b6054d56 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -9,8 +9,8 @@ function basetype end abstract type Enum{T<:Integer} end -Base.convert(::Type{Integer}, x::Enum{T}) where {T<:Integer} = bitcast(T, x) -Base.convert(::Type{T}, x::Enum{T2}) where {T<:Integer,T2<:Integer} = convert(T, bitcast(T2, x)) +Integer(x::Enum{T}) where {T<:Integer} = bitcast(T, x) +(::Type{T})(x::Enum{T2}) where {T<:Integer,T2<:Integer} = convert(T, bitcast(T2, x)) Base.write(io::IO, x::Enum{T}) where {T<:Integer} = write(io, T(x)) Base.read(io::IO, ::Type{T}) where {T<:Enum} = T(read(io, Enums.basetype(T))) @@ -106,7 +106,7 @@ macro enum(T, syms...) blk = quote # enum definition Base.@__doc__(primitive type $(esc(typename)) <: Enum{$(basetype)} $(sizeof(basetype) * 8) end) - function Base.convert(::Type{$(esc(typename))}, x::Integer) + function $(esc(typename))(x::Integer) $(membershiptest(:x, values)) || enum_argument_error($(Expr(:quote, typename)), x) return bitcast($(esc(typename)), convert($(basetype), x)) end diff --git a/base/bool.jl b/base/bool.jl index 2e78c2f824fcb1..9aa93dc484d4aa 100644 --- a/base/bool.jl +++ b/base/bool.jl @@ -1,11 +1,5 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -## boolean conversions ## - -convert(::Type{Bool}, x::Bool) = x -convert(::Type{Bool}, x::Float16) = x==0 ? false : x==1 ? true : throw(InexactError(:convert, Bool, x)) -convert(::Type{Bool}, x::Real) = x==0 ? false : x==1 ? true : throw(InexactError(:convert, Bool, x)) - # promote Bool to any other numeric type promote_rule(::Type{Bool}, ::Type{T}) where {T<:Number} = T diff --git a/base/boot.jl b/base/boot.jl index bca86c97463733..778a04f0a29bb7 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -605,4 +605,21 @@ Int(x::Ptr) = bitcast(Int, x) UInt(x::Ptr) = bitcast(UInt, x) Ptr{T}(x::Union{Int,UInt,Ptr}) where {T} = bitcast(Ptr{T}, x) +Signed(x::UInt8) = Int8(x) +Unsigned(x::Int8) = UInt8(x) +Signed(x::UInt16) = Int16(x) +Unsigned(x::Int16) = UInt16(x) +Signed(x::UInt32) = Int32(x) +Unsigned(x::Int32) = UInt32(x) +Signed(x::UInt64) = Int64(x) +Unsigned(x::Int64) = UInt64(x) +Signed(x::UInt128) = Int128(x) +Unsigned(x::Int128) = UInt128(x) + +Signed(x::Union{Float32, Float64, Bool}) = Int(x) +Unsigned(x::Union{Float32, Float64, Bool}) = UInt(x) + +Integer(x::Integer) = x +Integer(x::Real) = Signed(x) + ccall(:jl_set_istopmod, Void, (Any, Bool), Core, true) diff --git a/base/char.jl b/base/char.jl index ea7334eb0679e3..4d9a2662e946bf 100644 --- a/base/char.jl +++ b/base/char.jl @@ -1,8 +1,9 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -convert(::Type{Char}, x::UInt32) = reinterpret(Char, x) -convert(::Type{Char}, x::Number) = Char(UInt32(x)) -convert(::Type{UInt32}, x::Char) = reinterpret(UInt32, x) +Char(x::UInt32) = reinterpret(Char, x) +Char(x::Number) = Char(UInt32(x)) +convert(::Type{Char}, x::Number) = Char(x) +UInt32(x::Char) = reinterpret(UInt32, x) convert(::Type{T}, x::Char) where {T<:Number} = convert(T, UInt32(x)) rem(x::Char, ::Type{T}) where {T<:Number} = rem(UInt32(x), T) diff --git a/base/complex.jl b/base/complex.jl index 0664f9679c2485..31e7a7406105f3 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -32,13 +32,12 @@ const Complex128 = Complex{Float64} const Complex64 = Complex{Float32} const Complex32 = Complex{Float16} -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, T, z)) +Complex{T}(x::Real) where {T<:Real} = Complex{T}(x,0) +Complex{T}(z::Complex) where {T<:Real} = Complex{T}(real(z),imag(z)) +(::Type{T})(z::Complex) where {T<:Real} = + isreal(z) ? convert(T,real(z)) : throw(InexactError(Symbol(string(T)), T, z)) -convert(::Type{Complex}, z::Complex) = z -convert(::Type{Complex}, x::Real) = Complex(x) +Complex(z::Complex) = z promote_rule(::Type{Complex{T}}, ::Type{S}) where {T<:Real,S<:Real} = Complex{promote_type(T,S)} diff --git a/base/deprecated.jl b/base/deprecated.jl index 3428b14900e8ca..8f8b9fef31525a 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1704,6 +1704,18 @@ export hex2num @deprecate convert(::Type{String}, v::Vector{UInt8}) String(v) @deprecate convert(::Type{S}, g::UTF8proc.GraphemeIterator) where {S<:AbstractString} convert(S, g.s) +@deprecate convert(::Type{UInt128}, u::Random.UUID) UInt128(u) +@deprecate convert(::Type{Random.UUID}, s::AbstractString) Random.UUID(s) +@deprecate convert(::Type{Libc.FILE}, s::IO) Libc.FILE(s) +@deprecate convert(::Type{VersionNumber}, v::Integer) VersionNumber(v) +@deprecate convert(::Type{VersionNumber}, v::Tuple) VersionNumber(v) +@deprecate convert(::Type{VersionNumber}, v::AbstractString) VersionNumber(v) + +@deprecate (convert(::Type{Integer}, x::Enum{T}) where {T<:Integer}) Integer(x) +@deprecate (convert(::Type{T}, x::Enum{T2}) where {T<:Integer,T2<:Integer}) T(x) + +@deprecate convert(dt::Type{<:Integer}, ip::IPAddr) dt(ip) + # issue #5148, PR #23259 # warning for `const` on locals should be changed to an error in julia-syntax.scm diff --git a/base/float.jl b/base/float.jl index 4d65e56be4dede..f0ca0df9814146 100644 --- a/base/float.jl +++ b/base/float.jl @@ -45,7 +45,7 @@ A not-a-number value of type [`Float64`](@ref). const NaN = NaN64 ## conversions to floating-point ## -convert(::Type{Float16}, x::Integer) = convert(Float16, convert(Float32, x)) +Float16(x::Integer) = convert(Float16, convert(Float32, x)) for t in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128) @eval promote_rule(::Type{Float16}, ::Type{$t}) = Float16 end @@ -54,27 +54,28 @@ promote_rule(::Type{Float16}, ::Type{Bool}) = Float16 for t1 in (Float32, Float64) for st in (Int8, Int16, Int32, Int64) @eval begin - convert(::Type{$t1}, x::($st)) = sitofp($t1, x) + (::Type{$t1})(x::($st)) = sitofp($t1, x) promote_rule(::Type{$t1}, ::Type{$st}) = $t1 end end for ut in (Bool, UInt8, UInt16, UInt32, UInt64) @eval begin - convert(::Type{$t1}, x::($ut)) = uitofp($t1, x) + (::Type{$t1})(x::($ut)) = uitofp($t1, x) promote_rule(::Type{$t1}, ::Type{$ut}) = $t1 end end end -convert(::Type{Integer}, x::Float16) = convert(Integer, Float32(x)) -convert(::Type{T}, x::Float16) where {T<:Integer} = convert(T, Float32(x)) +Integer(x::Float16) = Integer(Float32(x)) +(::Type{T})(x::Float16) where {T<:Integer} = T(Float32(x)) +Bool(x::Real) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x)) promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64 promote_rule(::Type{Float64}, ::Type{Int128}) = Float64 promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32 promote_rule(::Type{Float32}, ::Type{Int128}) = Float32 -function convert(::Type{Float64}, x::UInt128) +function Float64(x::UInt128) x == 0 && return 0.0 n = 128-leading_zeros(x) # ndigits0z(x,2) if n <= 53 @@ -88,7 +89,7 @@ function convert(::Type{Float64}, x::UInt128) reinterpret(Float64, d + y) end -function convert(::Type{Float64}, x::Int128) +function Float64(x::Int128) x == 0 && return 0.0 s = ((x >>> 64) % UInt64) & 0x8000_0000_0000_0000 # sign bit x = abs(x) % UInt128 @@ -104,7 +105,7 @@ function convert(::Type{Float64}, x::Int128) reinterpret(Float64, s | d + y) end -function convert(::Type{Float32}, x::UInt128) +function Float32(x::UInt128) x == 0 && return 0f0 n = 128-leading_zeros(x) # ndigits0z(x,2) if n <= 24 @@ -118,7 +119,7 @@ function convert(::Type{Float32}, x::UInt128) reinterpret(Float32, d + y) end -function convert(::Type{Float32}, x::Int128) +function Float32(x::Int128) x == 0 && return 0f0 s = ((x >>> 96) % UInt32) & 0x8000_0000 # sign bit x = abs(x) % UInt128 @@ -134,7 +135,7 @@ function convert(::Type{Float32}, x::Int128) reinterpret(Float32, s | d + y) end -function convert(::Type{Float16}, val::Float32) +function Float16(val::Float32) f = reinterpret(UInt32, val) if isnan(val) t = 0x8000 ⊻ (0x8000 & ((f >> 0x10) % UInt16)) @@ -157,7 +158,7 @@ function convert(::Type{Float16}, val::Float32) reinterpret(Float16, h) end -function convert(::Type{Float32}, val::Float16) +function Float32(val::Float16) local ival::UInt32 = reinterpret(UInt16, val) local sign::UInt32 = (ival & 0x8000) >> 15 local exp::UInt32 = (ival & 0x7c00) >> 10 @@ -235,25 +236,28 @@ for i = 0:255 shifttable[i|0x100+1] = 13 end end + #convert(::Type{Float16}, x::Float32) = fptrunc(Float16, x) -convert(::Type{Float32}, x::Float64) = fptrunc(Float32, x) -convert(::Type{Float16}, x::Float64) = convert(Float16, convert(Float32, x)) +Float32(x::Float64) = fptrunc(Float32, x) +Float16(x::Float64) = Float16(Float32(x)) #convert(::Type{Float32}, x::Float16) = fpext(Float32, x) -convert(::Type{Float64}, x::Float32) = fpext(Float64, x) -convert(::Type{Float64}, x::Float16) = convert(Float64, convert(Float32, x)) - -convert(::Type{AbstractFloat}, x::Bool) = convert(Float64, x) -convert(::Type{AbstractFloat}, x::Int8) = convert(Float64, x) -convert(::Type{AbstractFloat}, x::Int16) = convert(Float64, x) -convert(::Type{AbstractFloat}, x::Int32) = convert(Float64, x) -convert(::Type{AbstractFloat}, x::Int64) = convert(Float64, x) # LOSSY -convert(::Type{AbstractFloat}, x::Int128) = convert(Float64, x) # LOSSY -convert(::Type{AbstractFloat}, x::UInt8) = convert(Float64, x) -convert(::Type{AbstractFloat}, x::UInt16) = convert(Float64, x) -convert(::Type{AbstractFloat}, x::UInt32) = convert(Float64, x) -convert(::Type{AbstractFloat}, x::UInt64) = convert(Float64, x) # LOSSY -convert(::Type{AbstractFloat}, x::UInt128) = convert(Float64, x) # LOSSY +Float64(x::Float32) = fpext(Float64, x) +Float64(x::Float16) = Float64(Float32(x)) + +AbstractFloat(x::Bool) = Float64(x) +AbstractFloat(x::Int8) = Float64(x) +AbstractFloat(x::Int16) = Float64(x) +AbstractFloat(x::Int32) = Float64(x) +AbstractFloat(x::Int64) = Float64(x) # LOSSY +AbstractFloat(x::Int128) = Float64(x) # LOSSY +AbstractFloat(x::UInt8) = Float64(x) +AbstractFloat(x::UInt16) = Float64(x) +AbstractFloat(x::UInt32) = Float64(x) +AbstractFloat(x::UInt64) = Float64(x) # LOSSY +AbstractFloat(x::UInt128) = Float64(x) # LOSSY + +Bool(x::Float16) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x)) """ float(x) @@ -261,7 +265,7 @@ convert(::Type{AbstractFloat}, x::UInt128) = convert(Float64, x) # LOSSY Convert a number or array to a floating point data type. When passed a string, this function is equivalent to `parse(Float64, x)`. """ -float(x) = convert(AbstractFloat, x) +float(x) = AbstractFloat(x) """ float(T::Type) @@ -675,11 +679,11 @@ for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UIn throw(InexactError(:trunc, $Ti, x)) end end - function convert(::Type{$Ti}, x::$Tf) + function (::Type{$Ti})(x::$Tf) if ($(Tf(typemin(Ti))) <= x <= $(Tf(typemax(Ti)))) && (trunc(x) == x) return unsafe_trunc($Ti,x) else - throw(InexactError(:convert, $Ti, x)) + throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x)) end end end @@ -696,11 +700,11 @@ for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UIn throw(InexactError(:trunc, $Ti, x)) end end - function convert(::Type{$Ti}, x::$Tf) + function (::Type{$Ti})(x::$Tf) if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))) && (trunc(x) == x) return unsafe_trunc($Ti,x) else - throw(InexactError(:convert, $Ti, x)) + throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x)) end end end diff --git a/base/gmp.jl b/base/gmp.jl index bf12ec54013079..05a391864c9724 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -231,7 +231,7 @@ widen(::Type{BigInt}) = BigInt signed(x::BigInt) = x -convert(::Type{BigInt}, x::BigInt) = x +BigInt(x::BigInt) = x hastypemax(::Type{BigInt}) = false @@ -263,14 +263,14 @@ function tryparse_internal(::Type{BigInt}, s::AbstractString, startpos::Int, end Nullable(flipsign!(z, sgn)) end -convert(::Type{BigInt}, x::Union{Clong,Int32}) = MPZ.set_si(x) -convert(::Type{BigInt}, x::Union{Culong,UInt32}) = MPZ.set_ui(x) -convert(::Type{BigInt}, x::Bool) = BigInt(UInt(x)) +BigInt(x::Union{Clong,Int32}) = MPZ.set_si(x) +BigInt(x::Union{Culong,UInt32}) = MPZ.set_ui(x) +BigInt(x::Bool) = BigInt(UInt(x)) unsafe_trunc(::Type{BigInt}, x::Union{Float32,Float64}) = MPZ.set_d(x) -function convert(::Type{BigInt}, x::Union{Float32,Float64}) - isinteger(x) || throw(InexactError(:convert, BigInt, x)) +function BigInt(x::Union{Float32,Float64}) + isinteger(x) || throw(InexactError(:BigInt, BigInt, x)) unsafe_trunc(BigInt,x) end @@ -279,10 +279,10 @@ function trunc(::Type{BigInt}, x::Union{Float32,Float64}) unsafe_trunc(BigInt,x) end -convert(::Type{BigInt}, x::Float16) = BigInt(Float64(x)) -convert(::Type{BigInt}, x::Float32) = BigInt(Float64(x)) +BigInt(x::Float16) = BigInt(Float64(x)) +BigInt(x::Float32) = BigInt(Float64(x)) -function convert(::Type{BigInt}, x::Integer) +function BigInt(x::Integer) if x < 0 if typemin(Clong) <= x return BigInt(convert(Clong,x)) @@ -324,26 +324,26 @@ function rem(x::BigInt, ::Type{T}) where T<:Union{Unsigned,Signed} flipsign(u, x.size) end -rem(x::Integer, ::Type{BigInt}) = convert(BigInt, x) +rem(x::Integer, ::Type{BigInt}) = BigInt(x) -function convert(::Type{T}, x::BigInt) where T<:Unsigned +function (::Type{T})(x::BigInt) where T<:Unsigned if sizeof(T) < sizeof(Limb) convert(T, convert(Limb,x)) else - 0 <= x.size <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(:convert, T, x)) + 0 <= x.size <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(Symbol(string(T)), T, x)) x % T end end -function convert(::Type{T}, x::BigInt) where T<:Signed +function (::Type{T})(x::BigInt) where T<:Signed n = abs(x.size) if sizeof(T) < sizeof(Limb) SLimb = typeof(Signed(one(Limb))) convert(T, convert(SLimb, x)) else - 0 <= n <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(:convert, T, x)) + 0 <= n <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(Symbol(string(T)), T, x)) y = x % T - ispos(x) ⊻ (y > 0) && throw(InexactError(:convert, T, x)) # catch overflow + ispos(x) ⊻ (y > 0) && throw(InexactError(Symbol(string(T)), T, x)) # catch overflow y end end @@ -386,9 +386,9 @@ function (::Type{T})(n::BigInt, ::RoundingMode{:Nearest}) where T<:CdoubleMax x end -convert(::Type{Float64}, n::BigInt) = Float64(n,RoundNearest) -convert(::Type{Float32}, n::BigInt) = Float32(n,RoundNearest) -convert(::Type{Float16}, n::BigInt) = Float16(n,RoundNearest) +Float64(n::BigInt) = Float64(n, RoundNearest) +Float32(n::BigInt) = Float32(n, RoundNearest) +Float16(n::BigInt) = Float16(n, RoundNearest) promote_rule(::Type{BigInt}, ::Type{<:Integer}) = BigInt diff --git a/base/int.jl b/base/int.jl index ec19107cde7c0e..278b6c27368db7 100644 --- a/base/int.jl +++ b/base/int.jl @@ -31,9 +31,6 @@ const BitUnsigned64T = Union{Type{UInt8}, Type{UInt16}, Type{UInt32}, Type{UIn const BitIntegerType = Union{map(T->Type{T}, BitInteger_types)...} -# calls constructors defined in boot.jl -convert(T::BitIntegerType, x::Union{BitInteger, Bool}) = T(x) - ## integer comparisons ## (<)(x::T, y::T) where {T<:BitSigned} = slt_int(x, y) @@ -466,16 +463,6 @@ rem(x::Integer, ::Type{Bool}) = ((x & 1) != 0) mod(x::Integer, ::Type{T}) where {T<:Integer} = rem(x, T) unsafe_trunc(::Type{T}, x::Integer) where {T<:Integer} = rem(x, T) -for (Ts, Tu) in ((Int8, UInt8), (Int16, UInt16), (Int32, UInt32), (Int64, UInt64), (Int128, UInt128)) - @eval convert(::Type{Signed}, x::$Tu) = convert($Ts, x) - @eval convert(::Type{Unsigned}, x::$Ts) = convert($Tu, x) -end - -convert(::Type{Signed}, x::Union{Float32, Float64, Bool}) = convert(Int, x) -convert(::Type{Unsigned}, x::Union{Float32, Float64, Bool}) = convert(UInt, x) - -convert(::Type{Integer}, x::Integer) = x -convert(::Type{Integer}, x::Real) = convert(Signed, x) """ trunc([T,] x, [digits, [base]]) diff --git a/base/irrationals.jl b/base/irrationals.jl index 27b94a59689033..a48b333668b10d 100644 --- a/base/irrationals.jl +++ b/base/irrationals.jl @@ -16,11 +16,11 @@ promote_rule(::Type{<:Irrational}, ::Type{Float32}) = Float32 promote_rule(::Type{<:Irrational}, ::Type{<:Irrational}) = Float64 promote_rule(::Type{<:Irrational}, ::Type{T}) where {T<:Number} = promote_type(Float64, T) -convert(::Type{AbstractFloat}, x::Irrational) = Float64(x) -convert(::Type{Float16}, x::Irrational) = Float16(Float32(x)) -convert(::Type{Complex{T}}, x::Irrational) where {T<:Real} = convert(Complex{T}, convert(T,x)) +AbstractFloat(x::Irrational) = Float64(x) +Float16(x::Irrational) = Float16(Float32(x)) +Complex{T}(x::Irrational) where {T<:Real} = Complex{T}(T(x)) -@pure function convert(::Type{Rational{T}}, x::Irrational) where T<:Integer +@pure function Rational{T}(x::Irrational) where T<:Integer o = precision(BigFloat) p = 256 while true @@ -34,7 +34,7 @@ convert(::Type{Complex{T}}, x::Irrational) where {T<:Real} = convert(Complex{T}, p += 32 end end -convert(::Type{Rational{BigInt}}, x::Irrational) = throw(ArgumentError("Cannot convert an Irrational to a Rational{BigInt}: use rationalize(Rational{BigInt}, x) instead")) +(::Type{Rational{BigInt}})(x::Irrational) = throw(ArgumentError("Cannot convert an Irrational to a Rational{BigInt}: use rationalize(Rational{BigInt}, x) instead")) @pure function (t::Type{T})(x::Irrational, r::RoundingMode) where T<:Union{Float32,Float64} setprecision(BigFloat, 256) do @@ -118,7 +118,7 @@ macro irrational(sym, val, def) esym = esc(sym) qsym = esc(Expr(:quote, sym)) bigconvert = isa(def,Symbol) ? quote - function Base.convert(::Type{BigFloat}, ::Irrational{$qsym}) + function Base.BigFloat(::Irrational{$qsym}) c = BigFloat() ccall(($(string("mpfr_const_", def)), :libmpfr), Cint, (Ptr{BigFloat}, Int32), @@ -126,20 +126,20 @@ macro irrational(sym, val, def) return c end end : quote - Base.convert(::Type{BigFloat}, ::Irrational{$qsym}) = $(esc(def)) + Base.BigFloat(::Irrational{$qsym}) = $(esc(def)) end quote const $esym = Irrational{$qsym}() $bigconvert - Base.convert(::Type{Float64}, ::Irrational{$qsym}) = $val - Base.convert(::Type{Float32}, ::Irrational{$qsym}) = $(Float32(val)) + Base.Float64(::Irrational{$qsym}) = $val + Base.Float32(::Irrational{$qsym}) = $(Float32(val)) @assert isa(big($esym), BigFloat) @assert Float64($esym) == Float64(big($esym)) @assert Float32($esym) == Float32(big($esym)) end end -big(x::Irrational) = convert(BigFloat,x) +big(x::Irrational) = BigFloat(x) big(::Type{<:Irrational}) = BigFloat ## specific irrational mathematical constants diff --git a/base/libc.jl b/base/libc.jl index 810cda11e448f1..1fb8b4644663bf 100644 --- a/base/libc.jl +++ b/base/libc.jl @@ -80,7 +80,6 @@ end Base.unsafe_convert(T::Union{Type{Ptr{Void}},Type{Ptr{FILE}}}, f::FILE) = convert(T, f.ptr) Base.close(f::FILE) = systemerror("fclose", ccall(:fclose, Cint, (Ptr{Void},), f.ptr) != 0) -Base.convert(::Type{FILE}, s::IO) = FILE(s) function Base.seek(h::FILE, offset::Integer) systemerror("fseek", ccall(:fseek, Cint, (Ptr{Void}, Clong, Cint), diff --git a/base/mpfr.jl b/base/mpfr.jl index 05129d39cdc8d0..1e8abd447bdcef 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -98,12 +98,12 @@ BigFloat(x) widen(::Type{Float64}) = BigFloat widen(::Type{BigFloat}) = BigFloat -convert(::Type{BigFloat}, x::BigFloat) = x +BigFloat(x::BigFloat) = x # convert to BigFloat for (fJ, fC) in ((:si,:Clong), (:ui,:Culong), (:d,:Float64)) @eval begin - function convert(::Type{BigFloat}, x::($fC)) + function BigFloat(x::($fC)) z = BigFloat() ccall(($(string(:mpfr_set_,fJ)), :libmpfr), Int32, (Ptr{BigFloat}, ($fC), Int32), &z, x, ROUNDING_MODE[]) return z @@ -111,19 +111,19 @@ for (fJ, fC) in ((:si,:Clong), (:ui,:Culong), (:d,:Float64)) end end -function convert(::Type{BigFloat}, x::BigInt) +function BigFloat(x::BigInt) z = BigFloat() ccall((:mpfr_set_z, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigInt}, Int32), &z, &x, ROUNDING_MODE[]) return z end -convert(::Type{BigFloat}, x::Integer) = BigFloat(BigInt(x)) +BigFloat(x::Integer) = BigFloat(BigInt(x)) -convert(::Type{BigFloat}, x::Union{Bool,Int8,Int16,Int32}) = BigFloat(convert(Clong,x)) -convert(::Type{BigFloat}, x::Union{UInt8,UInt16,UInt32}) = BigFloat(convert(Culong,x)) +BigFloat(x::Union{Bool,Int8,Int16,Int32}) = BigFloat(convert(Clong,x)) +BigFloat(x::Union{UInt8,UInt16,UInt32}) = BigFloat(convert(Culong,x)) -convert(::Type{BigFloat}, x::Union{Float16,Float32}) = BigFloat(Float64(x)) -convert(::Type{BigFloat}, x::Rational) = BigFloat(numerator(x)) / BigFloat(denominator(x)) +BigFloat(x::Union{Float16,Float32}) = BigFloat(Float64(x)) +BigFloat(x::Rational) = BigFloat(numerator(x)) / BigFloat(denominator(x)) function tryparse(::Type{BigFloat}, s::AbstractString, base::Int=0) z = BigFloat() @@ -131,8 +131,8 @@ function tryparse(::Type{BigFloat}, s::AbstractString, base::Int=0) err == 0 ? Nullable(z) : Nullable{BigFloat}() end -convert(::Type{Rational}, x::BigFloat) = convert(Rational{BigInt}, x) -convert(::Type{AbstractFloat}, x::BigInt) = BigFloat(x) +Rational(x::BigFloat) = convert(Rational{BigInt}, x) +AbstractFloat(x::BigInt) = BigFloat(x) float(::Type{BigInt}) = BigFloat @@ -237,29 +237,28 @@ floor(::Type{Integer}, x::BigFloat) = floor(BigInt, x) ceil(::Type{Integer}, x::BigFloat) = ceil(BigInt, x) round(::Type{Integer}, x::BigFloat) = round(BigInt, x) -convert(::Type{Bool}, x::BigFloat) = x==0 ? false : x==1 ? true : - throw(InexactError(:convert, Bool, x)) -function convert(::Type{BigInt},x::BigFloat) - isinteger(x) || throw(InexactError(:convert, BigInt, x)) +Bool(x::BigFloat) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x)) +function BigInt(x::BigFloat) + isinteger(x) || throw(InexactError(:BigInt, BigInt, x)) trunc(BigInt,x) end -function convert(::Type{Integer}, x::BigFloat) - isinteger(x) || throw(InexactError(:convert, Integer, x)) +function Integer(x::BigFloat) + isinteger(x) || throw(InexactError(:Integer, Integer, x)) trunc(Integer,x) end -function convert(::Type{T},x::BigFloat) where T<:Integer - isinteger(x) || throw(InexactError(:convert, T, x)) +function (::Type{T})(x::BigFloat) where T<:Integer + isinteger(x) || throw(InexactError(Symbol(string(T)), T, x)) trunc(T,x) end ## BigFloat -> AbstractFloat -convert(::Type{Float64}, x::BigFloat) = +Float64(x::BigFloat) = ccall((:mpfr_get_d,:libmpfr), Float64, (Ptr{BigFloat}, Int32), &x, ROUNDING_MODE[]) -convert(::Type{Float32}, x::BigFloat) = +Float32(x::BigFloat) = ccall((:mpfr_get_flt,:libmpfr), Float32, (Ptr{BigFloat}, Int32), &x, ROUNDING_MODE[]) # TODO: avoid double rounding -convert(::Type{Float16}, x::BigFloat) = convert(Float16, convert(Float32, x)) +Float16(x::BigFloat) = convert(Float16, convert(Float32, x)) Float64(x::BigFloat, r::RoundingMode) = ccall((:mpfr_get_d,:libmpfr), Float64, (Ptr{BigFloat}, Int32), &x, to_mpfr(r)) @@ -275,7 +274,7 @@ promote_rule(::Type{BigFloat}, ::Type{<:AbstractFloat}) = BigFloat big(::Type{<:AbstractFloat}) = BigFloat -function convert(::Type{Rational{BigInt}}, x::AbstractFloat) +function (::Type{Rational{BigInt}})(x::AbstractFloat) if isnan(x); return zero(BigInt)//zero(BigInt); end if isinf(x); return copysign(one(BigInt),x)//zero(BigInt); end if x == 0; return zero(BigInt) // one(BigInt); end diff --git a/base/number.jl b/base/number.jl index 4fb47f8c54cf3a..b510850e36ef82 100644 --- a/base/number.jl +++ b/base/number.jl @@ -1,6 +1,11 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license ## generic operations on numbers ## + +# Numbers are convertible +convert(::Type{T}, x::T) where {T<:Number} = x +convert(::Type{T}, x::Number) where {T<:Number} = T(x) + """ isinteger(x) -> Bool diff --git a/base/random/misc.jl b/base/random/misc.jl index 0d5f1123a41fc0..39cb798856a72b 100644 --- a/base/random/misc.jl +++ b/base/random/misc.jl @@ -433,9 +433,9 @@ julia> Base.Random.uuid_version(Base.Random.uuid4(rng)) """ uuid_version(u::UUID) = Int((u.value >> 76) & 0xf) -Base.convert(::Type{UInt128}, u::UUID) = u.value +UInt128(u::UUID) = u.value -function Base.convert(::Type{UUID}, s::AbstractString) +function UUID(s::AbstractString) s = lowercase(s) if !ismatch(r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$", s) diff --git a/base/rational.jl b/base/rational.jl index 87c0c2cb0cdd9c..19a44d5b6555c1 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -74,32 +74,31 @@ function write(s::IO, z::Rational) write(s,numerator(z),denominator(z)) end -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)) +Rational{T}(x::Rational) where {T<:Integer} = Rational{T}(convert(T,x.num), convert(T,x.den)) +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) +Rational(x::Rational) = x -convert(::Type{Bool}, x::Rational) = x==0 ? false : x==1 ? true : - throw(InexactError(:convert, Bool, x)) # to resolve ambiguity -convert(::Type{Integer}, x::Rational) = (isinteger(x) ? convert(Integer, x.num) : - throw(InexactError(:convert, Integer, x))) -convert(::Type{T}, x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num) : - throw(InexactError(:convert, T, x))) +Bool(x::Rational) = x==0 ? false : x==1 ? true : + throw(InexactError(:Bool, Bool, x)) # to resolve ambiguity +Integer(x::Rational) = (isinteger(x) ? convert(Integer, x.num) : + throw(InexactError(:Integer, Integer, x))) +(::Type{T})(x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num) : + throw(InexactError(Symbol(string(T)), T, x))) -convert(::Type{AbstractFloat}, x::Rational) = float(x.num)/float(x.den) -function convert(::Type{T}, x::Rational{S}) where T<:AbstractFloat where S +AbstractFloat(x::Rational) = float(x.num)/float(x.den) +function (::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(::Type{Rational{T}}, x::AbstractFloat) where T<:Integer +function Rational{T}(x::AbstractFloat) where T<:Integer r = rationalize(T, x, tol=0) - x == convert(typeof(x), r) || throw(InexactError(:convert, Rational{T}, x)) + x == convert(typeof(x), r) || throw(InexactError(:Rational, Rational{T}, x)) r end -convert(::Type{Rational}, x::Float64) = convert(Rational{Int64}, x) -convert(::Type{Rational}, x::Float32) = convert(Rational{Int}, x) +Rational(x::Float64) = Rational{Int64}(x) +Rational(x::Float32) = Rational{Int}(x) big(z::Complex{<:Rational{<:Integer}}) = Complex{Rational{BigInt}}(z) diff --git a/base/socket.jl b/base/socket.jl index 588e0097a69a5a..d80fe920b3262e 100644 --- a/base/socket.jl +++ b/base/socket.jl @@ -4,7 +4,7 @@ abstract type IPAddr end Base.isless(a::T, b::T) where {T<:IPAddr} = isless(a.host, b.host) -Base.convert(dt::Type{<:Integer}, ip::IPAddr) = dt(ip.host) +(dt::Type{<:Integer})(ip::IPAddr) = dt(ip.host) struct IPv4 <: IPAddr host::UInt32 diff --git a/base/twiceprecision.jl b/base/twiceprecision.jl index 1baf1b9a921bd4..3fab676190eea1 100644 --- a/base/twiceprecision.jl +++ b/base/twiceprecision.jl @@ -240,12 +240,12 @@ promote_rule(::Type{TwicePrecision{R}}, ::Type{TwicePrecision{S}}) where {R,S} = promote_rule(::Type{TwicePrecision{R}}, ::Type{S}) where {R,S} = TwicePrecision{promote_type(R,S)} -convert(::Type{TwicePrecision{T}}, x::TwicePrecision{T}) where {T} = x -convert(::Type{TwicePrecision{T}}, x::TwicePrecision) where {T} = +TwicePrecision{T}(x::TwicePrecision{T}) where {T} = x +TwicePrecision{T}(x::TwicePrecision) where {T} = TwicePrecision{T}(convert(T, x.hi), convert(T, x.lo)) -convert(::Type{T}, x::TwicePrecision) where {T<:Number} = convert(T, x.hi + x.lo) -convert(::Type{TwicePrecision{T}}, x::Number) where {T} = TwicePrecision{T}(convert(T, x), zero(T)) +(::Type{T})(x::TwicePrecision) where {T<:Number} = convert(T, x.hi + x.lo) +TwicePrecision{T}(x::Number) where {T} = TwicePrecision{T}(convert(T, x), zero(T)) float(x::TwicePrecision{<:AbstractFloat}) = x float(x::TwicePrecision) = TwicePrecision(float(x.hi), float(x.lo)) diff --git a/base/version.jl b/base/version.jl index 9e18099faeae4b..2be6ff530f5a85 100644 --- a/base/version.jl +++ b/base/version.jl @@ -47,6 +47,8 @@ VersionNumber(major::Integer, minor::Integer = 0, patch::Integer = 0, map(x->x isa Integer ? UInt64(x) : String(x), pre), map(x->x isa Integer ? UInt64(x) : String(x), bld)) +VersionNumber(v::Tuple) = VersionNumber(v...) + function print(io::IO, v::VersionNumber) v == typemax(VersionNumber) && return print(io, "∞") print(io, v.major) @@ -65,9 +67,6 @@ function print(io::IO, v::VersionNumber) end show(io::IO, v::VersionNumber) = print(io, "v\"", v, "\"") -convert(::Type{VersionNumber}, v::Integer) = VersionNumber(v) -convert(::Type{VersionNumber}, v::Tuple) = VersionNumber(v...) - const VERSION_REGEX = r"^ v? # prefix (optional) (\d+) # major (required) @@ -104,8 +103,6 @@ function VersionNumber(v::AbstractString) return VersionNumber(major, minor, patch, prerl, build) end -convert(::Type{VersionNumber}, v::AbstractString) = VersionNumber(v) - macro v_str(v); VersionNumber(v); end typemin(::Type{VersionNumber}) = v"0-" @@ -215,7 +212,7 @@ A `VersionNumber` object describing which version of Julia is in use. For detail [Version Number Literals](@ref man-version-number-literals). """ const VERSION = try - ver = convert(VersionNumber, VERSION_STRING) + ver = VersionNumber(VERSION_STRING) if !isempty(ver.prerelease) if GIT_VERSION_INFO.build_number >= 0 ver = VersionNumber(ver.major, ver.minor, ver.patch, (ver.prerelease..., GIT_VERSION_INFO.build_number), ver.build) @@ -232,7 +229,7 @@ catch e VersionNumber(0) end -const libllvm_version = convert(VersionNumber, libllvm_version_string) +const libllvm_version = VersionNumber(libllvm_version_string) function banner(io::IO = STDOUT) if GIT_VERSION_INFO.tagged_commit diff --git a/test/enums.jl b/test/enums.jl index ea4169af7b8aa6..157b7efd7991bb 100644 --- a/test/enums.jl +++ b/test/enums.jl @@ -26,19 +26,19 @@ using Base.Test @test_throws MethodError Fruit(0.0) @test typemin(Fruit) == apple @test typemax(Fruit) == kiwi -@test convert(Fruit,0) == apple -@test convert(Fruit,1) == orange -@test convert(Fruit,2) == kiwi -@test_throws ArgumentError convert(Fruit,3) -@test_throws ArgumentError convert(Fruit,-1) -@test convert(UInt8,apple) === 0x00 -@test convert(UInt16,orange) === 0x0001 -@test convert(UInt128,kiwi) === 0x00000000000000000000000000000002 -@test typeof(convert(BigInt,apple)) <: BigInt -@test convert(BigInt,apple) == 0 -@test convert(Bool,apple) == false -@test convert(Bool,orange) == true -@test_throws InexactError convert(Bool,kiwi) +@test Fruit(0) == apple +@test Fruit(1) == orange +@test Fruit(2) == kiwi +@test_throws ArgumentError Fruit(3) +@test_throws ArgumentError Fruit(-1) +@test UInt8(apple) === 0x00 +@test UInt16(orange) === 0x0001 +@test UInt128(kiwi) === 0x00000000000000000000000000000002 +@test typeof(BigInt(apple)) <: BigInt +@test BigInt(apple) == 0 +@test Bool(apple) == false +@test Bool(orange) == true +@test_throws InexactError Bool(kiwi) @test instances(Fruit) == (apple, orange, kiwi) f(x::Fruit) = "hey, I'm a Fruit" @@ -66,7 +66,7 @@ end @enum Negative _neg1=-1 _neg2=-2 @test Int(_neg1) === -1 @test Int(_neg2) === -2 -@test_throws InexactError convert(UInt8, _neg1) +@test_throws InexactError UInt8(_neg1) @enum Negative2 _neg5=-5 _neg4 _neg3 @test Int(_neg5) === -5 @test Int(_neg4) === -4 @@ -84,7 +84,7 @@ end # other Integer types of enum members @enum Test3::UInt8 _one_Test3=0x01 _two_Test3=0x02 _three_Test3=0x03 @test Test3.size == 1 -@test convert(UInt8, _one_Test3) === 0x01 +@test UInt8(_one_Test3) === 0x01 @test length(instances(Test3)) == 3 @enum Test4::UInt16 _one_Test4=0x01 _two_Test4=0x0002 _three_Test4=0x03 @@ -95,7 +95,7 @@ end @enum Test6::UInt128 _one_Test6=0x00000000000000000000000000000001 _two_Test6=0x00000000000000000000000000000002 @test Test6.size == 16 -@test typeof(convert(Integer, _one_Test6)) == UInt128 +@test typeof(Integer(_one_Test6)) == UInt128 # enum values must be integers @test_throws ArgumentError eval(:(@enum Test7 _zero="zero")) diff --git a/test/version.jl b/test/version.jl index 6bc50a54843b70..df7170f3b57829 100644 --- a/test/version.jl +++ b/test/version.jl @@ -82,15 +82,15 @@ io = IOBuffer() show(io,v"4.3.2+1.a") @test length(String(take!(io))) == 12 -# conversion from Int -@test convert(VersionNumber, 2) == v"2.0.0" +# construction from Int +@test VersionNumber(2) == v"2.0.0" -# conversion from Tuple -@test convert(VersionNumber, (2,)) == v"2.0.0" -@test convert(VersionNumber, (3, 2)) == v"3.2.0" +# construction from Tuple +@test VersionNumber((2,)) == v"2.0.0" +@test VersionNumber((3, 2)) == v"3.2.0" -# conversion from AbstractString -@test convert(VersionNumber, "4.3.2+1.a") == v"4.3.2+1.a" +# construction from AbstractString +@test VersionNumber("4.3.2+1.a") == v"4.3.2+1.a" # typemin and typemax @test typemin(VersionNumber) == v"0-"