Skip to content

Commit

Permalink
Merge pull request #21499 from musm/new
Browse files Browse the repository at this point in the history
New where syntax in misc places
  • Loading branch information
Sacha0 committed Apr 23, 2017
2 parents 57bcefb + cb42a51 commit 226e2dc
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 101 deletions.
10 changes: 5 additions & 5 deletions base/broadcast.jl
Expand Up @@ -135,7 +135,7 @@ Base.@propagate_inbounds _broadcast_getindex(::Any, A, I) = A[I]
## Broadcasting core
# nargs encodes the number of As arguments (which matches the number
# of keeps). The first two type parameters are to ensure specialization.
@generated function _broadcast!{K,ID,AT,BT,N}(f, B::AbstractArray, keeps::K, Idefaults::ID, A::AT, Bs::BT, ::Type{Val{N}}, iter)
@generated function _broadcast!(f, B::AbstractArray, keeps::K, Idefaults::ID, A::AT, Bs::BT, ::Type{Val{N}}, iter) where {K,ID,AT,BT,N}
nargs = N + 1
quote
$(Expr(:meta, :inline))
Expand All @@ -158,7 +158,7 @@ end

# For BitArray outputs, we cache the result in a "small" Vector{Bool},
# and then copy in chunks into the output
@generated function _broadcast!{K,ID,AT,BT,N}(f, B::BitArray, keeps::K, Idefaults::ID, A::AT, Bs::BT, ::Type{Val{N}}, iter)
@generated function _broadcast!(f, B::BitArray, keeps::K, Idefaults::ID, A::AT, Bs::BT, ::Type{Val{N}}, iter) where {K,ID,AT,BT,N}
nargs = N + 1
quote
$(Expr(:meta, :inline))
Expand Down Expand Up @@ -213,7 +213,7 @@ as in `broadcast!(f, A, A, B)` to perform `A[:] = broadcast(f, A, B)`.
end

# broadcast with computed element type
@generated function _broadcast!{K,ID,AT,nargs}(f, B::AbstractArray, keeps::K, Idefaults::ID, As::AT, ::Type{Val{nargs}}, iter, st, count)
@generated function _broadcast!(f, B::AbstractArray, keeps::K, Idefaults::ID, As::AT, ::Type{Val{nargs}}, iter, st, count) where {K,ID,AT,nargs}
quote
$(Expr(:meta, :noinline))
# destructure the keeps and As tuples
Expand Down Expand Up @@ -262,7 +262,7 @@ function broadcast_t(f, ::Type{Any}, shape, iter, As...)
B[I] = val
return _broadcast!(f, B, keeps, Idefaults, As, Val{nargs}, iter, st, 1)
end
@inline function broadcast_t{N}(f, T, shape, iter, A, Bs::Vararg{Any,N})
@inline function broadcast_t(f, T, shape, iter, A, Bs::Vararg{Any,N}) where N
C = similar(Array{T}, shape)
keeps, Idefaults = map_newindexer(shape, A, Bs)
_broadcast!(f, C, keeps, Idefaults, A, Bs, Val{N}, iter)
Expand All @@ -273,7 +273,7 @@ end
# in the common case where this is used for logical array indexing; in
# performance-critical cases where Array{Bool} is desired, one can always
# use broadcast! instead.
@inline function broadcast_t{N}(f, ::Type{Bool}, shape, iter, A, Bs::Vararg{Any,N})
@inline function broadcast_t(f, ::Type{Bool}, shape, iter, A, Bs::Vararg{Any,N}) where N
C = similar(BitArray, shape)
keeps, Idefaults = map_newindexer(shape, A, Bs)
_broadcast!(f, C, keeps, Idefaults, A, Bs, Val{N}, iter)
Expand Down
80 changes: 40 additions & 40 deletions base/checked.jl
Expand Up @@ -27,7 +27,7 @@ checked_cld(x::Integer, y::Integer) = checked_cld(promote(x,y)...)

# fallback catchall rules to prevent infinite recursion if promotion succeeds,
# but no method exists to handle those types
checked_abs{T<:Integer}(x::T) = no_op_err("checked_abs", T)
checked_abs(x::T) where {T<:Integer} = no_op_err("checked_abs", T)

const SignedInt = Union{Int8,Int16,Int32,Int64,Int128}
const UnsignedInt = Union{UInt8,UInt16,UInt32,UInt64,UInt128}
Expand Down Expand Up @@ -87,7 +87,7 @@ represent `-typemin(Int)`, thus leading to an overflow.
The overflow protection may impose a perceptible performance penalty.
"""
function checked_neg{T<:Integer}(x::T)
function checked_neg(x::T) where T<:Integer
checked_sub(T(0), x)
end
if BrokenSignedInt != Union{}
Expand All @@ -98,7 +98,7 @@ function checked_neg(x::BrokenSignedInt)
end
end
if BrokenUnsignedInt != Union{}
function checked_neg{T<:BrokenUnsignedInt}(x::T)
function checked_neg(x::T) where T<:BrokenUnsignedInt
x != 0 && throw(OverflowError())
T(0)
end
Expand Down Expand Up @@ -131,20 +131,20 @@ checked_abs(x::Bool) = x
Calculates `r = x+y`, with the flag `f` indicating whether overflow has occurred.
"""
function add_with_overflow end
add_with_overflow{T<:SignedInt}(x::T, y::T) = checked_sadd_int(x, y)
add_with_overflow{T<:UnsignedInt}(x::T, y::T) = checked_uadd_int(x, y)
add_with_overflow(x::Bool, y::Bool) = x+y, false
add_with_overflow(x::T, y::T) where {T<:SignedInt} = checked_sadd_int(x, y)
add_with_overflow(x::T, y::T) where {T<:UnsignedInt} = checked_uadd_int(x, y)
add_with_overflow(x::Bool, y::Bool) = (x+y, false)

if BrokenSignedInt != Union{}
function add_with_overflow{T<:BrokenSignedInt}(x::T, y::T)
function add_with_overflow(x::T, y::T) where T<:BrokenSignedInt
r = x + y
# x and y have the same sign, and the result has a different sign
f = (x<0) == (y<0) != (r<0)
r, f
end
end
if BrokenUnsignedInt != Union{}
function add_with_overflow{T<:BrokenUnsignedInt}(x::T, y::T)
function add_with_overflow(x::T, y::T) where T<:BrokenUnsignedInt
# x + y > typemax(T)
# Note: ~y == -y-1
x + y, x > ~y
Expand All @@ -159,7 +159,7 @@ Calculates `x+y`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
function checked_add{T<:Integer}(x::T, y::T)
function checked_add(x::T, y::T) where T<:Integer
@_inline_meta
z, b = add_with_overflow(x, y)
b && throw(OverflowError())
Expand All @@ -170,17 +170,17 @@ end
checked_add(x) = x
checked_add(x::Bool) = +x

checked_add{T}(x1::T, x2::T, x3::T) =
checked_add(x1::T, x2::T, x3::T) where {T} =
checked_add(checked_add(x1, x2), x3)
checked_add{T}(x1::T, x2::T, x3::T, x4::T) =
checked_add(x1::T, x2::T, x3::T, x4::T) where {T} =
checked_add(checked_add(x1, x2), x3, x4)
checked_add{T}(x1::T, x2::T, x3::T, x4::T, x5::T) =
checked_add(x1::T, x2::T, x3::T, x4::T, x5::T) where {T} =
checked_add(checked_add(x1, x2), x3, x4, x5)
checked_add{T}(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T) =
checked_add(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T) where {T} =
checked_add(checked_add(x1, x2), x3, x4, x5, x6)
checked_add{T}(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T) =
checked_add(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T) where {T} =
checked_add(checked_add(x1, x2), x3, x4, x5, x6, x7)
checked_add{T}(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T, x8::T) =
checked_add(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T, x8::T) where {T} =
checked_add(checked_add(x1, x2), x3, x4, x5, x6, x7, x8)


Expand All @@ -190,20 +190,20 @@ checked_add{T}(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T, x8::T) =
Calculates `r = x-y`, with the flag `f` indicating whether overflow has occurred.
"""
function sub_with_overflow end
sub_with_overflow{T<:SignedInt}(x::T, y::T) = checked_ssub_int(x, y)
sub_with_overflow{T<:UnsignedInt}(x::T, y::T) = checked_usub_int(x, y)
sub_with_overflow(x::Bool, y::Bool) = x-y, false
sub_with_overflow(x::T, y::T) where {T<:SignedInt} = checked_ssub_int(x, y)
sub_with_overflow(x::T, y::T) where {T<:UnsignedInt} = checked_usub_int(x, y)
sub_with_overflow(x::Bool, y::Bool) = (x-y, false)

if BrokenSignedInt != Union{}
function sub_with_overflow{T<:BrokenSignedInt}(x::T, y::T)
function sub_with_overflow(x::T, y::T) where T<:BrokenSignedInt
r = x - y
# x and y have different signs, and the result has a different sign than x
f = (x<0) != (y<0) == (r<0)
r, f
end
end
if BrokenUnsignedInt != Union{}
function sub_with_overflow{T<:BrokenUnsignedInt}(x::T, y::T)
function sub_with_overflow(x::T, y::T) where T<:BrokenUnsignedInt
# x - y < 0
x - y, x < y
end
Expand All @@ -216,7 +216,7 @@ Calculates `x-y`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
function checked_sub{T<:Integer}(x::T, y::T)
function checked_sub(x::T, y::T) where T<:Integer
@_inline_meta
z, b = sub_with_overflow(x, y)
b && throw(OverflowError())
Expand All @@ -230,27 +230,27 @@ end
Calculates `r = x*y`, with the flag `f` indicating whether overflow has occurred.
"""
function mul_with_overflow end
mul_with_overflow{T<:SignedInt}(x::T, y::T) = checked_smul_int(x, y)
mul_with_overflow{T<:UnsignedInt}(x::T, y::T) = checked_umul_int(x, y)
mul_with_overflow(x::Bool, y::Bool) = x*y, false
mul_with_overflow(x::T, y::T) where {T<:SignedInt} = checked_smul_int(x, y)
mul_with_overflow(x::T, y::T) where {T<:UnsignedInt} = checked_umul_int(x, y)
mul_with_overflow(x::Bool, y::Bool) = (x*y, false)

if BrokenSignedIntMul != Union{} && BrokenSignedIntMul != Int128
function mul_with_overflow{T<:BrokenSignedIntMul}(x::T, y::T)
function mul_with_overflow(x::T, y::T) where T<:BrokenSignedIntMul
r = widemul(x, y)
f = r % T != r
r % T, f
end
end
if BrokenUnsignedIntMul != Union{} && BrokenUnsignedIntMul != UInt128
function mul_with_overflow{T<:BrokenUnsignedIntMul}(x::T, y::T)
function mul_with_overflow(x::T, y::T) where T<:BrokenUnsignedIntMul
r = widemul(x, y)
f = r % T != r
r % T, f
end
end
if Int128 <: BrokenSignedIntMul
# Avoid BigInt
function mul_with_overflow{T<:Int128}(x::T, y::T)
function mul_with_overflow(x::T, y::T) where T<:Int128
f = if y > 0
# x * y > typemax(T)
# x * y < typemin(T)
Expand All @@ -268,7 +268,7 @@ if Int128 <: BrokenSignedIntMul
end
if UInt128 <: BrokenUnsignedIntMul
# Avoid BigInt
function mul_with_overflow{T<:UInt128}(x::T, y::T)
function mul_with_overflow(x::T, y::T) where T<:UInt128
# x * y > typemax(T)
x * y, y > 0 && x > fld(typemax(T), y)
end
Expand All @@ -281,7 +281,7 @@ Calculates `x*y`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
function checked_mul{T<:Integer}(x::T, y::T)
function checked_mul(x::T, y::T) where T<:Integer
@_inline_meta
z, b = mul_with_overflow(x, y)
b && throw(OverflowError())
Expand All @@ -290,17 +290,17 @@ end

# Handle multiple arguments
checked_mul(x) = x
checked_mul{T}(x1::T, x2::T, x3::T) =
checked_mul(x1::T, x2::T, x3::T) where {T} =
checked_mul(checked_mul(x1, x2), x3)
checked_mul{T}(x1::T, x2::T, x3::T, x4::T) =
checked_mul(x1::T, x2::T, x3::T, x4::T) where {T} =
checked_mul(checked_mul(x1, x2), x3, x4)
checked_mul{T}(x1::T, x2::T, x3::T, x4::T, x5::T) =
checked_mul(x1::T, x2::T, x3::T, x4::T, x5::T) where {T} =
checked_mul(checked_mul(x1, x2), x3, x4, x5)
checked_mul{T}(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T) =
checked_mul(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T) where {T} =
checked_mul(checked_mul(x1, x2), x3, x4, x5, x6)
checked_mul{T}(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T) =
checked_mul(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T) where {T} =
checked_mul(checked_mul(x1, x2), x3, x4, x5, x6, x7)
checked_mul{T}(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T, x8::T) =
checked_mul(x1::T, x2::T, x3::T, x4::T, x5::T, x6::T, x7::T, x8::T) where {T} =
checked_mul(checked_mul(x1, x2), x3, x4, x5, x6, x7, x8)

"""
Expand All @@ -310,7 +310,7 @@ Calculates `div(x,y)`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
checked_div{T<:Integer}(x::T, y::T) = div(x, y) # Base.div already checks
checked_div(x::T, y::T) where {T<:Integer} = div(x, y) # Base.div already checks

"""
Base.checked_rem(x, y)
Expand All @@ -319,7 +319,7 @@ Calculates `x%y`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
checked_rem{T<:Integer}(x::T, y::T) = rem(x, y) # Base.rem already checks
checked_rem(x::T, y::T) where {T<:Integer} = rem(x, y) # Base.rem already checks

"""
Base.checked_fld(x, y)
Expand All @@ -328,7 +328,7 @@ Calculates `fld(x,y)`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
checked_fld{T<:Integer}(x::T, y::T) = fld(x, y) # Base.fld already checks
checked_fld(x::T, y::T) where {T<:Integer} = fld(x, y) # Base.fld already checks

"""
Base.checked_mod(x, y)
Expand All @@ -337,7 +337,7 @@ Calculates `mod(x,y)`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
checked_mod{T<:Integer}(x::T, y::T) = mod(x, y) # Base.mod already checks
checked_mod(x::T, y::T) where {T<:Integer} = mod(x, y) # Base.mod already checks

"""
Base.checked_cld(x, y)
Expand All @@ -346,6 +346,6 @@ Calculates `cld(x,y)`, checking for overflow errors where applicable.
The overflow protection may impose a perceptible performance penalty.
"""
checked_cld{T<:Integer}(x::T, y::T) = cld(x, y) # Base.cld already checks
checked_cld(x::T, y::T) where {T<:Integer} = cld(x, y) # Base.cld already checks

end
22 changes: 11 additions & 11 deletions base/complex.jl
Expand Up @@ -31,7 +31,7 @@ promote_rule(::Type{Complex{T}}, ::Type{S}) where {T<:Real,S<:Real} =
promote_rule(::Type{Complex{T}}, ::Type{Complex{S}}) where {T<:Real,S<:Real} =
Complex{promote_type(T,S)}

widen{T}(::Type{Complex{T}}) = Complex{widen(T)}
widen(::Type{Complex{T}}) where {T} = Complex{widen(T)}

"""
real(z)
Expand Down Expand Up @@ -134,8 +134,8 @@ julia> complex(Int)
Complex{Int64}
```
"""
complex{T<:Real}(::Type{T}) = Complex{T}
complex{T<:Real}(::Type{Complex{T}}) = Complex{T}
complex(::Type{T}) where {T<:Real} = Complex{T}
complex(::Type{Complex{T}}) where {T<:Real} = Complex{T}

flipsign(x::Complex, y::Real) = ifelse(signbit(y), -x, x)

Expand Down Expand Up @@ -589,13 +589,13 @@ function ^(z::Complex{T}, p::Complex{T})::Complex{T} where T<:AbstractFloat
end
end

function exp2{T}(z::Complex{T})
function exp2(z::Complex{T}) where T
er = exp2(real(z))
theta = imag(z) * log(convert(T, 2))
Complex(er*cos(theta), er*sin(theta))
end

function exp10{T}(z::Complex{T})
function exp10(z::Complex{T}) where T
er = exp10(real(z))
theta = imag(z) * log(convert(T, 10))
Complex(er*cos(theta), er*sin(theta))
Expand Down Expand Up @@ -669,7 +669,7 @@ end
n>=0 ? power_by_squaring(z,n) : power_by_squaring(inv(z),-n)
^(z::Complex{<:Integer}, n::Integer) = power_by_squaring(z,n) # DomainError for n<0

function sin{T}(z::Complex{T})
function sin(z::Complex{T}) where T
F = float(T)
zr, zi = reim(z)
if zr == 0
Expand All @@ -686,7 +686,7 @@ function sin{T}(z::Complex{T})
end


function cos{T}(z::Complex{T})
function cos(z::Complex{T}) where T
F = float(T)
zr, zi = reim(z)
if zr == 0
Expand Down Expand Up @@ -764,7 +764,7 @@ function cosh(z::Complex)
cos(Complex(zi,-zr))
end

function tanh{T<:AbstractFloat}(z::Complex{T})
function tanh(z::Complex{T}) where T<:AbstractFloat
const Ω = prevfloat(typemax(T))
ξ, η = reim(z)
if isnan(ξ) && η==0 return Complex(ξ, η) end
Expand Down Expand Up @@ -809,7 +809,7 @@ function acosh(z::Complex)
Complex(ξ, η)
end

function atanh{T<:AbstractFloat}(z::Complex{T})
function atanh(z::Complex{T}) where T<:AbstractFloat
const Ω = prevfloat(typemax(T))
const θ = sqrt(Ω)/4
const ρ = 1/θ
Expand Down Expand Up @@ -866,7 +866,7 @@ breaking ties using the specified [`RoundingMode`](@ref)s. The first
[`RoundingMode`](@ref) is used for rounding the real components while the
second is used for rounding the imaginary components.
"""
function round{MR, MI}(z::Complex{<:AbstractFloat}, ::RoundingMode{MR}, ::RoundingMode{MI})
function round(z::Complex{<:AbstractFloat}, ::RoundingMode{MR}, ::RoundingMode{MI}) where {MR,MI}
Complex(round(real(z), RoundingMode{MR}()),
round(imag(z), RoundingMode{MI}()))
end
Expand All @@ -887,7 +887,7 @@ big(z::Complex{<:Integer}) = Complex{BigInt}(z)

complex(A::AbstractArray{<:Complex}) = A

function complex{T}(A::AbstractArray{T})
function complex(A::AbstractArray{T}) where T
if !isleaftype(T)
error("`complex` not defined on abstractly-typed arrays; please convert to a more specific type")
end
Expand Down
2 changes: 1 addition & 1 deletion base/dict.jl
Expand Up @@ -213,7 +213,7 @@ isslotempty(h::Dict, i::Int) = h.slots[i] == 0x0
isslotfilled(h::Dict, i::Int) = h.slots[i] == 0x1
isslotmissing(h::Dict, i::Int) = h.slots[i] == 0x2

function rehash!{K,V}(h::Dict{K,V}, newsz = length(h.keys))
function rehash!(h::Dict{K,V}, newsz = length(h.keys)) where V where K
olds = h.slots
oldk = h.keys
oldv = h.vals
Expand Down

0 comments on commit 226e2dc

Please sign in to comment.