diff --git a/NEWS.md b/NEWS.md index dba780a656031..c2f3e5612fb54 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,6 +20,18 @@ New language features Language changes ---------------- + * "Inner constructor" syntax for parametric types is deprecated. For example, + in this definition: + ``` + type Foo{T} + x + Foo(x) = new(x) + end + ``` + the syntax `Foo(x) = new(x)` actually defined a constructor for `Foo{T}`, + i.e. the case where the type parameter is specified. For clarity, this + definition now must be written as `Foo{T}(x) where T = new(x)`. ([#11310]) + * Multi-line and single-line nonstandard command literals have been added. A nonstandard command literal is like a nonstandard string literal, but the syntax uses backquotes (``` ` ```) instead of double quotes, and the diff --git a/base/LineEdit.jl b/base/LineEdit.jl index 3a952c4bd193b..85af843b4f59d 100644 --- a/base/LineEdit.jl +++ b/base/LineEdit.jl @@ -1014,10 +1014,10 @@ type HistoryPrompt{T<:HistoryProvider} <: TextInterface hp::T complete keymap_dict::Dict{Char,Any} - HistoryPrompt(hp) = new(hp, EmptyCompletionProvider()) + HistoryPrompt{T}(hp) where T<:HistoryProvider = new(hp, EmptyCompletionProvider()) end -HistoryPrompt{T<:HistoryProvider}(hp::T) = HistoryPrompt{T}(hp) +HistoryPrompt(hp::T) where T<:HistoryProvider = HistoryPrompt{T}(hp) init_state(terminal, p::HistoryPrompt) = SearchState(terminal, p, true, IOBuffer(), IOBuffer()) type PrefixSearchState <: ModeState @@ -1054,10 +1054,11 @@ type PrefixHistoryPrompt{T<:HistoryProvider} <: TextInterface parent_prompt::Prompt complete keymap_dict::Dict{Char,Any} - PrefixHistoryPrompt(hp, parent_prompt) = new(hp, parent_prompt, EmptyCompletionProvider()) + PrefixHistoryPrompt{T}(hp, parent_prompt) where T<:HistoryProvider = + new(hp, parent_prompt, EmptyCompletionProvider()) end -PrefixHistoryPrompt{T<:HistoryProvider}(hp::T, parent_prompt) = PrefixHistoryPrompt{T}(hp, parent_prompt) +PrefixHistoryPrompt(hp::T, parent_prompt) where T<:HistoryProvider = PrefixHistoryPrompt{T}(hp, parent_prompt) init_state(terminal, p::PrefixHistoryPrompt) = PrefixSearchState(terminal, p, "", IOBuffer()) write_prompt(terminal, s::PrefixSearchState) = write_prompt(terminal, s.histprompt.parent_prompt) diff --git a/base/atomics.jl b/base/atomics.jl index f2cd218d9a607..676059ae56d17 100644 --- a/base/atomics.jl +++ b/base/atomics.jl @@ -58,8 +58,8 @@ Atomic operations use an `atomic_` prefix, such as `atomic_add!`, """ type Atomic{T<:AtomicTypes} value::T - Atomic() = new(zero(T)) - Atomic(value) = new(value) + Atomic{T}() where T<:AtomicTypes = new(zero(T)) + Atomic{T}(value) where T<:AtomicTypes = new(value) end Atomic() = Atomic{Int}() diff --git a/base/base.jl b/base/base.jl index 45d8de10d8281..403863bf33fa0 100644 --- a/base/base.jl +++ b/base/base.jl @@ -152,6 +152,6 @@ immutable Nullable{T} hasvalue::Bool value::T - Nullable() = new(false) - Nullable(value::T, hasvalue::Bool=true) = new(hasvalue, value) + Nullable{T}() where T = new(false) + Nullable{T}(value::T, hasvalue::Bool=true) where T = new(hasvalue, value) end diff --git a/base/bitarray.jl b/base/bitarray.jl index e3f71da10f0d8..1b2548288a8d0 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -8,7 +8,7 @@ type BitArray{N} <: DenseArray{Bool, N} chunks::Vector{UInt64} len::Int dims::NTuple{N,Int} - function BitArray(dims::Vararg{Int,N}) + function BitArray{N}(dims::Vararg{Int,N}) where N n = 1 i = 1 for d in dims @@ -36,7 +36,7 @@ Construct an uninitialized `BitArray` with the given dimensions. Behaves identically to the [`Array`](@ref) constructor. """ BitArray(dims::Integer...) = BitArray(map(Int,dims)) -BitArray{N}(dims::NTuple{N,Int}) = BitArray{N}(dims...) +BitArray(dims::NTuple{N,Int}) where N = BitArray{N}(dims...) typealias BitVector BitArray{1} typealias BitMatrix BitArray{2} diff --git a/base/boot.jl b/base/boot.jl index 1ab6488471a9b..0fc64e620053b 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -269,9 +269,9 @@ Void() = nothing immutable VecElement{T} value::T - VecElement(value::T) = new(value) # disable converting constructor in Core + VecElement{T}(value::T) where T = new(value) # disable converting constructor in Core end -VecElement{T}(arg::T) = VecElement{T}(arg) +VecElement(arg::T) where T = VecElement{T}(arg) # used by lowering of splicing unquote splicedexpr(hd::Symbol, args::Array{Any,1}) = (e=Expr(hd); e.args=args; e) diff --git a/base/channels.jl b/base/channels.jl index 61cdf753568d3..84ee87ff9a9c8 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -29,14 +29,14 @@ type Channel{T} <: AbstractChannel # Used when sz_max == 0, i.e., an unbuffered channel. takers::Array{Condition} - function Channel(sz::Float64) + function Channel{T}(sz::Float64) where T if sz == Inf Channel{T}(typemax(Int)) else Channel{T}(convert(Int, sz)) end end - function Channel(sz::Integer) + function Channel{T}(sz::Integer) where T if sz < 0 throw(ArgumentError("Channel size must be either 0, a positive integer or Inf")) end @@ -44,7 +44,7 @@ type Channel{T} <: AbstractChannel end # deprecated empty constructor - function Channel() + function Channel{T}() where T depwarn(string("The empty constructor Channel() is deprecated. ", "The channel size needs to be specified explictly. ", "Defaulting to Channel{$T}(32)."), :Channel) @@ -364,7 +364,7 @@ show(io::IO, c::Channel) = print(io, "$(typeof(c))(sz_max:$(c.sz_max),sz_curr:$( type ChannelIterState{T} hasval::Bool val::T - ChannelIterState(x) = new(x) + ChannelIterState{T}(has::Bool) where T = new(has) end start{T}(c::Channel{T}) = ChannelIterState{T}(false) diff --git a/base/clusterserialize.jl b/base/clusterserialize.jl index 93d2e2e5cbe47..ad7dde9e5277b 100644 --- a/base/clusterserialize.jl +++ b/base/clusterserialize.jl @@ -16,8 +16,10 @@ type ClusterSerializer{I<:IO} <: AbstractSerializer # anonymous functions. anonfunc_id::UInt64 - ClusterSerializer(io::I) = new(io, 0, ObjectIdDict(), Base.worker_id_from_socket(io), - Set{UInt64}(), Dict{UInt64, UInt64}(), Dict{UInt64, Vector{Symbol}}(), 0) + function ClusterSerializer{I}(io::I) where I<:IO + new(io, 0, ObjectIdDict(), Base.worker_id_from_socket(io), + Set{UInt64}(), Dict{UInt64, UInt64}(), Dict{UInt64, Vector{Symbol}}(), 0) + end end ClusterSerializer(io::IO) = ClusterSerializer{typeof(io)}(io) diff --git a/base/dft.jl b/base/dft.jl index 5ffeeaa46467f..11660bba8b539 100644 --- a/base/dft.jl +++ b/base/dft.jl @@ -241,10 +241,10 @@ type ScaledPlan{T,P,N} <: Plan{T} p::P scale::N # not T, to avoid unnecessary promotion to Complex pinv::Plan - ScaledPlan(p, scale) = new(p, scale) + ScaledPlan{T,P,N}(p, scale) where (T,P,N) = new(p, scale) end -(::Type{ScaledPlan{T}}){T,P,N}(p::P, scale::N) = ScaledPlan{T,P,N}(p, scale) -ScaledPlan{T}(p::Plan{T}, scale::Number) = ScaledPlan{T}(p, scale) +ScaledPlan{T}(p::P, scale::N) where (T,P,N) = ScaledPlan{T,P,N}(p, scale) +ScaledPlan(p::Plan{T}, scale::Number) where T = ScaledPlan{T}(p, scale) ScaledPlan(p::ScaledPlan, α::Number) = ScaledPlan(p.p, p.scale * α) size(p::ScaledPlan) = size(p.p) diff --git a/base/dict.jl b/base/dict.jl index 4138fe7310b0b..482e4252893dc 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -99,27 +99,11 @@ type Dict{K,V} <: Associative{K,V} idxfloor::Int # an index <= the indexes of all used slots maxprobe::Int - function Dict() + function Dict{K,V}() where V where K n = 16 new(zeros(UInt8,n), Array{K,1}(n), Array{V,1}(n), 0, 0, 0, 1, 0) end - function Dict(kv) - h = Dict{K,V}() - for (k,v) in kv - h[k] = v - end - return h - end - Dict(p::Pair) = setindex!(Dict{K,V}(), p.second, p.first) - function Dict(ps::Pair...) - h = Dict{K,V}() - sizehint!(h, length(ps)) - for p in ps - h[p.first] = p.second - end - return h - end - function Dict(d::Dict{K,V}) + function Dict{K,V}(d::Dict{K,V}) where V where K if d.ndel > 0 rehash!(d) end @@ -128,6 +112,22 @@ type Dict{K,V} <: Associative{K,V} d.maxprobe) end end +function Dict{K,V}(kv) where V where K + h = Dict{K,V}() + for (k,v) in kv + h[k] = v + end + return h +end +Dict{K,V}(p::Pair) where V where K = setindex!(Dict{K,V}(), p.second, p.first) +function Dict{K,V}(ps::Pair...) where V where K + h = Dict{K,V}() + sizehint!(h, length(ps)) + for p in ps + h[p.first] = p.second + end + return h +end # Note the constructors of WeakKeyDict mirror these here, keep in sync. Dict() = Dict{Any,Any}() Dict(kv::Tuple{}) = Dict() @@ -135,10 +135,10 @@ copy(d::Dict) = Dict(d) const AnyDict = Dict{Any,Any} -Dict{K,V}(ps::Pair{K,V}...) = Dict{K,V}(ps) -Dict{K }(ps::Pair{K}...,) = Dict{K,Any}(ps) -Dict{V }(ps::(Pair{K,V} where K)...,) = Dict{Any,V}(ps) -Dict( ps::Pair...) = Dict{Any,Any}(ps) +Dict(ps::Pair{K,V}...) where (K,V) = Dict{K,V}(ps) +Dict(ps::Pair{K}...,) where K = Dict{K,Any}(ps) +Dict(ps::(Pair{K,V} where K)...,) where V = Dict{Any,V}(ps) +Dict(ps::Pair...) = Dict{Any,Any}(ps) function Dict(kv) try @@ -599,9 +599,9 @@ immutable ImmutableDict{K, V} <: Associative{K,V} parent::ImmutableDict{K, V} key::K value::V - ImmutableDict() = new() # represents an empty dictionary - ImmutableDict(key, value) = (empty = new(); new(empty, key, value)) - ImmutableDict(parent::ImmutableDict, key, value) = new(parent, key, value) + ImmutableDict{K,V}() where (K,V) = new() # represents an empty dictionary + ImmutableDict{K,V}(key, value) where (K,V) = (empty = new(); new(empty, key, value)) + ImmutableDict{K,V}(parent::ImmutableDict, key, value) where (K,V) = new(parent, key, value) end """ @@ -621,8 +621,8 @@ Create a new entry in the Immutable Dictionary for the key => value pair """ ImmutableDict -ImmutableDict{K,V}(KV::Pair{K,V}) = ImmutableDict{K,V}(KV[1], KV[2]) -ImmutableDict{K,V}(t::ImmutableDict{K,V}, KV::Pair) = ImmutableDict{K,V}(t, KV[1], KV[2]) +ImmutableDict(KV::Pair{K,V}) where (K,V) = ImmutableDict{K,V}(KV[1], KV[2]) +ImmutableDict(t::ImmutableDict{K,V}, KV::Pair) where (K,V) = ImmutableDict{K,V}(t, KV[1], KV[2]) function in(key_value::Pair, dict::ImmutableDict, valcmp=(==)) key, value = key_value diff --git a/base/fft/FFTW.jl b/base/fft/FFTW.jl index c33fe02874472..11eb75785cf7a 100644 --- a/base/fft/FFTW.jl +++ b/base/fft/FFTW.jl @@ -211,8 +211,8 @@ for P in (:cFFTWPlan, :rFFTWPlan, :r2rFFTWPlan) # complex, r2c/c2r, and r2r flags::UInt32 # planner flags region::Any # region (iterable) of dims that are transormed pinv::ScaledPlan - function $P(plan::PlanPtr, flags::Integer, R::Any, - X::StridedArray{T, N}, Y::StridedArray) + function $P{T,K,inplace,N}(plan::PlanPtr, flags::Integer, R::Any, + X::StridedArray{T, N}, Y::StridedArray) where (T<:fftwNumber,K,inplace,N) p = new(plan, size(X), size(Y), strides(X), strides(Y), alignment_of(X), alignment_of(Y), flags, R) finalizer(p, destroy_plan) diff --git a/base/fft/dct.jl b/base/fft/dct.jl index 00e8675ffac97..0c86913929055 100644 --- a/base/fft/dct.jl +++ b/base/fft/dct.jl @@ -15,7 +15,7 @@ type DCTPlan{T<:fftwNumber,K,inplace} <: Plan{T} nrm::Float64 # normalization factor region::Dims # dimensions being transformed pinv::DCTPlan{T} - DCTPlan(plan,r,nrm,region) = new(plan,r,nrm,region) + DCTPlan{T,K,inplace}(plan,r,nrm,region) where (T<:fftwNumber,K,inplace) = new(plan,r,nrm,region) end size(p::DCTPlan) = size(p.plan) diff --git a/base/iobuffer.jl b/base/iobuffer.jl index a77c2f82bebf5..72af09462d340 100644 --- a/base/iobuffer.jl +++ b/base/iobuffer.jl @@ -14,13 +14,17 @@ type AbstractIOBuffer{T<:AbstractVector{UInt8}} <: IO ptr::Int # read (and maybe write) pointer mark::Int # reset mark location for ptr (or <0 for no mark) - AbstractIOBuffer(data::T,readable::Bool,writable::Bool,seekable::Bool,append::Bool,maxsize::Int) = + function AbstractIOBuffer{T}(data::T, readable::Bool, writable::Bool, seekable::Bool, append::Bool, + maxsize::Int) where T<:AbstractVector{UInt8} new(data,readable,writable,seekable,append,length(data),maxsize,1,-1) + end end typealias IOBuffer AbstractIOBuffer{Vector{UInt8}} -AbstractIOBuffer{T<:AbstractVector{UInt8}}(data::T, readable::Bool, writable::Bool, seekable::Bool, append::Bool, maxsize::Int) = +function AbstractIOBuffer(data::T, readable::Bool, writable::Bool, seekable::Bool, append::Bool, + maxsize::Int) where T<:AbstractVector{UInt8} AbstractIOBuffer{T}(data, readable, writable, seekable, append, maxsize) +end # allocate Vector{UInt8}s for IOBuffer storage that can efficiently become Strings StringVector(n::Integer) = Vector{UInt8}(_string_n(n)) diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index d6df493e79901..cc950b297c093 100644 --- a/base/linalg/arnoldi.jl +++ b/base/linalg/arnoldi.jl @@ -288,10 +288,10 @@ type SVDOperator{T<:BlasFloat,S} <: AbstractArray{T, 2} X::S m::Int n::Int - SVDOperator(X::AbstractMatrix) = new(X, size(X, 1), size(X, 2)) + SVDOperator{T,S}(X::AbstractMatrix) where (T<:BlasFloat,S) = new(X, size(X, 1), size(X, 2)) end -function SVDOperator{T}(A::AbstractMatrix{T}) +function SVDOperator(A::AbstractMatrix{T}) where T Tnew = typeof(zero(T)/sqrt(one(T))) Anew = convert(AbstractMatrix{Tnew}, A) SVDOperator{Tnew,typeof(Anew)}(Anew) diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index f040cd127c27d..0b2d5223c167b 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -5,7 +5,7 @@ type Bidiagonal{T} <: AbstractMatrix{T} dv::Vector{T} # diagonal ev::Vector{T} # sub/super diagonal isupper::Bool # is upper bidiagonal (true) or lower (false) - function Bidiagonal(dv::Vector{T}, ev::Vector{T}, isupper::Bool) + function Bidiagonal{T}(dv::Vector{T}, ev::Vector{T}, isupper::Bool) where T if length(ev) != length(dv)-1 throw(DimensionMismatch("length of diagonal vector is $(length(dv)), length of off-diagonal vector is $(length(ev))")) end @@ -52,7 +52,7 @@ julia> Bl = Bidiagonal(dv, ev, false) # ev is on the first subdiagonal ⋅ ⋅ 9 4 ``` """ -Bidiagonal{T}(dv::AbstractVector{T}, ev::AbstractVector{T}, isupper::Bool) = Bidiagonal{T}(collect(dv), collect(ev), isupper) +Bidiagonal(dv::AbstractVector{T}, ev::AbstractVector{T}, isupper::Bool) where T = Bidiagonal{T}(collect(dv), collect(ev), isupper) Bidiagonal(dv::AbstractVector, ev::AbstractVector) = throw(ArgumentError("did you want an upper or lower Bidiagonal? Try again with an additional true (upper) or false (lower) argument.")) """ @@ -106,7 +106,7 @@ Bidiagonal(dv::AbstractVector, ev::AbstractVector, uplo::Char) = begin end Bidiagonal(collect(dv), collect(ev), isupper) end -function Bidiagonal{Td,Te}(dv::AbstractVector{Td}, ev::AbstractVector{Te}, isupper::Bool) +function Bidiagonal(dv::AbstractVector{Td}, ev::AbstractVector{Te}, isupper::Bool) where (Td,Te) T = promote_type(Td,Te) Bidiagonal(convert(Vector{T}, dv), convert(Vector{T}, ev), isupper) end @@ -197,7 +197,7 @@ full(A::Bidiagonal) = convert(Array, A) promote_rule{T,S}(::Type{Matrix{T}}, ::Type{Bidiagonal{S}})=Matrix{promote_type(T,S)} #Converting from Bidiagonal to Tridiagonal -Tridiagonal{T}(M::Bidiagonal{T}) = convert(Tridiagonal{T}, M) +Tridiagonal(M::Bidiagonal{T}) where T = convert(Tridiagonal{T}, M) function convert{T}(::Type{Tridiagonal{T}}, A::Bidiagonal) z = zeros(T, size(A)[1]-1) A.isupper ? Tridiagonal(z, convert(Vector{T},A.dv), convert(Vector{T},A.ev)) : Tridiagonal(convert(Vector{T},A.ev), convert(Vector{T},A.dv), z) diff --git a/base/linalg/eigen.jl b/base/linalg/eigen.jl index 9386a7f0a65e5..a926f2c2757e0 100644 --- a/base/linalg/eigen.jl +++ b/base/linalg/eigen.jl @@ -4,17 +4,21 @@ immutable Eigen{T,V,S<:AbstractMatrix,U<:AbstractVector} <: Factorization{T} values::U vectors::S - Eigen(values::AbstractVector{V}, vectors::AbstractMatrix{T}) = new(values, vectors) + Eigen{T,V,S,U}(values::AbstractVector{V}, vectors::AbstractMatrix{T}) where (T,V,S,U) = + new(values, vectors) end -Eigen{T,V}(values::AbstractVector{V}, vectors::AbstractMatrix{T}) = Eigen{T,V,typeof(vectors),typeof(values)}(values, vectors) +Eigen(values::AbstractVector{V}, vectors::AbstractMatrix{T}) where (T,V) = + Eigen{T,V,typeof(vectors),typeof(values)}(values, vectors) # Generalized eigenvalue problem. immutable GeneralizedEigen{T,V,S<:AbstractMatrix,U<:AbstractVector} <: Factorization{T} values::U vectors::S - GeneralizedEigen(values::AbstractVector{V}, vectors::AbstractMatrix{T}) = new(values, vectors) + GeneralizedEigen{T,V,S,U}(values::AbstractVector{V}, vectors::AbstractMatrix{T}) where (T,V,S,U) = + new(values, vectors) end -GeneralizedEigen{T,V}(values::AbstractVector{V}, vectors::AbstractMatrix{T}) = GeneralizedEigen{T,V,typeof(vectors),typeof(values)}(values, vectors) +GeneralizedEigen(values::AbstractVector{V}, vectors::AbstractMatrix{T}) where (T,V) = + GeneralizedEigen{T,V,typeof(vectors),typeof(values)}(values, vectors) function getindex(A::Union{Eigen,GeneralizedEigen}, d::Symbol) diff --git a/base/linalg/hessenberg.jl b/base/linalg/hessenberg.jl index faa7c4813a569..326a853e71cb6 100644 --- a/base/linalg/hessenberg.jl +++ b/base/linalg/hessenberg.jl @@ -3,9 +3,10 @@ immutable Hessenberg{T,S<:AbstractMatrix} <: Factorization{T} factors::S τ::Vector{T} - Hessenberg(factors::AbstractMatrix{T}, τ::Vector{T}) = new(factors, τ) + Hessenberg{T,S}(factors::AbstractMatrix{T}, τ::Vector{T}) where (T,S<:AbstractMatrix) = + new(factors, τ) end -Hessenberg{T}(factors::AbstractMatrix{T}, τ::Vector{T}) = Hessenberg{T,typeof(factors)}(factors, τ) +Hessenberg(factors::AbstractMatrix{T}, τ::Vector{T}) where T = Hessenberg{T,typeof(factors)}(factors, τ) Hessenberg(A::StridedMatrix) = Hessenberg(LAPACK.gehrd!(A)...) @@ -55,7 +56,7 @@ end immutable HessenbergQ{T,S<:AbstractMatrix} <: AbstractMatrix{T} factors::S τ::Vector{T} - HessenbergQ(factors::AbstractMatrix{T}, τ::Vector{T}) = new(factors, τ) + HessenbergQ{T,S}(factors::AbstractMatrix{T}, τ::Vector{T}) where (T,S<:AbstractMatrix) = new(factors, τ) end HessenbergQ{T}(factors::AbstractMatrix{T}, τ::Vector{T}) = HessenbergQ{T,typeof(factors)}(factors, τ) HessenbergQ(A::Hessenberg) = HessenbergQ(A.factors, A.τ) diff --git a/base/linalg/lq.jl b/base/linalg/lq.jl index 81d06d2d6cd4d..5420f19b3feb0 100644 --- a/base/linalg/lq.jl +++ b/base/linalg/lq.jl @@ -5,17 +5,17 @@ immutable LQ{T,S<:AbstractMatrix} <: Factorization{T} factors::S τ::Vector{T} - LQ(factors::AbstractMatrix{T}, τ::Vector{T}) = new(factors, τ) + LQ{T,S}(factors::AbstractMatrix{T}, τ::Vector{T}) where (T,S<:AbstractMatrix) = new(factors, τ) end immutable LQPackedQ{T,S<:AbstractMatrix} <: AbstractMatrix{T} factors::Matrix{T} τ::Vector{T} - LQPackedQ(factors::AbstractMatrix{T}, τ::Vector{T}) = new(factors, τ) + LQPackedQ{T,S}(factors::AbstractMatrix{T}, τ::Vector{T}) where (T,S<:AbstractMatrix) = new(factors, τ) end -LQ{T}(factors::AbstractMatrix{T}, τ::Vector{T}) = LQ{T,typeof(factors)}(factors, τ) -LQPackedQ{T}(factors::AbstractMatrix{T}, τ::Vector{T}) = LQPackedQ{T,typeof(factors)}(factors, τ) +LQ(factors::AbstractMatrix{T}, τ::Vector{T}) where T = LQ{T,typeof(factors)}(factors, τ) +LQPackedQ(factors::AbstractMatrix{T}, τ::Vector{T}) where T = LQPackedQ{T,typeof(factors)}(factors, τ) """ lqfact!(A) -> LQ diff --git a/base/linalg/lu.jl b/base/linalg/lu.jl index 501d1542c5459..0292de0404678 100644 --- a/base/linalg/lu.jl +++ b/base/linalg/lu.jl @@ -7,9 +7,9 @@ immutable LU{T,S<:AbstractMatrix} <: Factorization{T} factors::S ipiv::Vector{BlasInt} info::BlasInt - LU(factors::AbstractMatrix{T}, ipiv::Vector{BlasInt}, info::BlasInt) = new(factors, ipiv, info) + LU{T,S}(factors::AbstractMatrix{T}, ipiv::Vector{BlasInt}, info::BlasInt) where (T,S) = new(factors, ipiv, info) end -LU{T}(factors::AbstractMatrix{T}, ipiv::Vector{BlasInt}, info::BlasInt) = LU{T,typeof(factors)}(factors, ipiv, info) +LU(factors::AbstractMatrix{T}, ipiv::Vector{BlasInt}, info::BlasInt) where T = LU{T,typeof(factors)}(factors, ipiv, info) # StridedMatrix function lufact!{T<:BlasFloat}(A::StridedMatrix{T}, pivot::Union{Type{Val{false}}, Type{Val{true}}} = Val{true}) diff --git a/base/linalg/qr.jl b/base/linalg/qr.jl index c27e8522a6e0f..51816b0dbef3a 100644 --- a/base/linalg/qr.jl +++ b/base/linalg/qr.jl @@ -5,24 +5,26 @@ immutable QR{T,S<:AbstractMatrix} <: Factorization{T} factors::S τ::Vector{T} - QR(factors::AbstractMatrix{T}, τ::Vector{T}) = new(factors, τ) + QR{T,S}(factors::AbstractMatrix{T}, τ::Vector{T}) where (T,S<:AbstractMatrix) = new(factors, τ) end -QR{T}(factors::AbstractMatrix{T}, τ::Vector{T}) = QR{T,typeof(factors)}(factors, τ) +QR(factors::AbstractMatrix{T}, τ::Vector{T}) where T = QR{T,typeof(factors)}(factors, τ) # Note. For QRCompactWY factorization without pivoting, the WY representation based method introduced in LAPACK 3.4 immutable QRCompactWY{S,M<:AbstractMatrix} <: Factorization{S} factors::M T::Matrix{S} - QRCompactWY(factors::AbstractMatrix{S}, T::AbstractMatrix{S}) = new(factors, T) + QRCompactWY{S,M}(factors::AbstractMatrix{S}, T::AbstractMatrix{S}) where (S,M<:AbstractMatrix) = new(factors, T) end -QRCompactWY{S}(factors::AbstractMatrix{S}, T::AbstractMatrix{S}) = QRCompactWY{S,typeof(factors)}(factors, T) +QRCompactWY(factors::AbstractMatrix{S}, T::AbstractMatrix{S}) where S = QRCompactWY{S,typeof(factors)}(factors, T) immutable QRPivoted{T,S<:AbstractMatrix} <: Factorization{T} factors::S τ::Vector{T} jpvt::Vector{BlasInt} - QRPivoted(factors::AbstractMatrix{T}, τ::Vector{T}, jpvt::Vector{BlasInt}) = new(factors, τ, jpvt) + QRPivoted{T,S}(factors::AbstractMatrix{T}, τ::Vector{T}, jpvt::Vector{BlasInt}) where (T,S<:AbstractMatrix) = + new(factors, τ, jpvt) end -QRPivoted{T}(factors::AbstractMatrix{T}, τ::Vector{T}, jpvt::Vector{BlasInt}) = QRPivoted{T,typeof(factors)}(factors, τ, jpvt) +QRPivoted(factors::AbstractMatrix{T}, τ::Vector{T}, jpvt::Vector{BlasInt}) where T = + QRPivoted{T,typeof(factors)}(factors, τ, jpvt) function qrfactUnblocked!{T}(A::AbstractMatrix{T}) m, n = size(A) @@ -342,16 +344,16 @@ getq(A::Union{QR, QRPivoted}) = QRPackedQ(A.factors,A.τ) immutable QRPackedQ{T,S<:AbstractMatrix} <: AbstractMatrix{T} factors::S τ::Vector{T} - QRPackedQ(factors::AbstractMatrix{T}, τ::Vector{T}) = new(factors, τ) + QRPackedQ{T,S}(factors::AbstractMatrix{T}, τ::Vector{T}) where (T,S<:AbstractMatrix) = new(factors, τ) end -QRPackedQ{T}(factors::AbstractMatrix{T}, τ::Vector{T}) = QRPackedQ{T,typeof(factors)}(factors, τ) +QRPackedQ(factors::AbstractMatrix{T}, τ::Vector{T}) where T = QRPackedQ{T,typeof(factors)}(factors, τ) immutable QRCompactWYQ{S, M<:AbstractMatrix} <: AbstractMatrix{S} factors::M T::Matrix{S} - QRCompactWYQ(factors::AbstractMatrix{S}, T::Matrix{S}) = new(factors, T) + QRCompactWYQ{S,M}(factors::AbstractMatrix{S}, T::Matrix{S}) where (S,M<:AbstractMatrix) = new(factors, T) end -QRCompactWYQ{S}(factors::AbstractMatrix{S}, T::Matrix{S}) = QRCompactWYQ{S,typeof(factors)}(factors, T) +QRCompactWYQ(factors::AbstractMatrix{S}, T::Matrix{S}) where S = QRCompactWYQ{S,typeof(factors)}(factors, T) convert{T}(::Type{QRPackedQ{T}}, Q::QRPackedQ) = QRPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) convert{T}(::Type{AbstractMatrix{T}}, Q::QRPackedQ{T}) = Q diff --git a/base/linalg/rowvector.jl b/base/linalg/rowvector.jl index c5d2839e19b3c..37f9f509f4ecd 100644 --- a/base/linalg/rowvector.jl +++ b/base/linalg/rowvector.jl @@ -15,7 +15,7 @@ scalar, but will otherwise behave similarly. """ immutable RowVector{T,V<:AbstractVector} <: AbstractMatrix{T} vec::V - function RowVector(v::V) + function RowVector{T,V}(v::V) where V<:AbstractVector where T check_types(T,v) new(v) end @@ -30,16 +30,16 @@ end @inline transpose_type{T}(::Type{T}) = promote_op(transpose, T) # Constructors that take a vector -@inline RowVector{T}(vec::AbstractVector{T}) = RowVector{transpose_type(T),typeof(vec)}(vec) -@inline (::Type{RowVector{T}}){T}(vec::AbstractVector{T}) = RowVector{T,typeof(vec)}(vec) +@inline RowVector(vec::AbstractVector{T}) where T = RowVector{transpose_type(T),typeof(vec)}(vec) +@inline RowVector{T}(vec::AbstractVector{T}) where T = RowVector{T,typeof(vec)}(vec) # Constructors that take a size and default to Array -@inline (::Type{RowVector{T}}){T}(n::Int) = RowVector{T}(Vector{transpose_type(T)}(n)) -@inline (::Type{RowVector{T}}){T}(n1::Int, n2::Int) = n1 == 1 ? +@inline RowVector{T}(n::Int) where T = RowVector{T}(Vector{transpose_type(T)}(n)) +@inline RowVector{T}(n1::Int, n2::Int) where T = n1 == 1 ? RowVector{T}(Vector{transpose_type(T)}(n2)) : error("RowVector expects 1×N size, got ($n1,$n2)") -@inline (::Type{RowVector{T}}){T}(n::Tuple{Int}) = RowVector{T}(Vector{transpose_type(T)}(n[1])) -@inline (::Type{RowVector{T}}){T}(n::Tuple{Int,Int}) = n[1] == 1 ? +@inline RowVector{T}(n::Tuple{Int}) where T = RowVector{T}(Vector{transpose_type(T)}(n[1])) +@inline RowVector{T}(n::Tuple{Int,Int}) where T = n[1] == 1 ? RowVector{T}(Vector{transpose_type(T)}(n[2])) : error("RowVector expects 1×N size, got $n") diff --git a/base/linalg/schur.jl b/base/linalg/schur.jl index cfd75d203f763..a4f5015ee64dc 100644 --- a/base/linalg/schur.jl +++ b/base/linalg/schur.jl @@ -5,9 +5,9 @@ immutable Schur{Ty,S<:AbstractMatrix} <: Factorization{Ty} T::S Z::S values::Vector - Schur(T::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}, values::Vector) = new(T, Z, values) + Schur{Ty,S}(T::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}, values::Vector) where (Ty,S) = new(T, Z, values) end -Schur{Ty}(T::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}, values::Vector) = Schur{Ty, typeof(T)}(T, Z, values) +Schur(T::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}, values::Vector) where Ty = Schur{Ty, typeof(T)}(T, Z, values) """ schurfact!(A::StridedMatrix) -> F::Schur @@ -151,9 +151,15 @@ immutable GeneralizedSchur{Ty,M<:AbstractMatrix} <: Factorization{Ty} beta::Vector{Ty} Q::M Z::M - GeneralizedSchur(S::AbstractMatrix{Ty}, T::AbstractMatrix{Ty}, alpha::Vector, beta::Vector{Ty}, Q::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}) = new(S, T, alpha, beta, Q, Z) + function GeneralizedSchur{Ty,M}(S::AbstractMatrix{Ty}, T::AbstractMatrix{Ty}, alpha::Vector, + beta::Vector{Ty}, Q::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}) where (Ty,M) + new(S, T, alpha, beta, Q, Z) + end +end +function GeneralizedSchur(S::AbstractMatrix{Ty}, T::AbstractMatrix{Ty}, alpha::Vector, + beta::Vector{Ty}, Q::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}) where Ty + GeneralizedSchur{Ty, typeof(S)}(S, T, alpha, beta, Q, Z) end -GeneralizedSchur{Ty}(S::AbstractMatrix{Ty}, T::AbstractMatrix{Ty}, alpha::Vector, beta::Vector{Ty}, Q::AbstractMatrix{Ty}, Z::AbstractMatrix{Ty}) = GeneralizedSchur{Ty, typeof(S)}(S, T, alpha, beta, Q, Z) """ schurfact!(A::StridedMatrix, B::StridedMatrix) -> F::GeneralizedSchur diff --git a/base/linalg/svd.jl b/base/linalg/svd.jl index f0ee0d3f38c8d..4cb0a3269ac23 100644 --- a/base/linalg/svd.jl +++ b/base/linalg/svd.jl @@ -5,9 +5,10 @@ immutable SVD{T,Tr,M<:AbstractArray} <: Factorization{T} U::M S::Vector{Tr} Vt::M - SVD(U::AbstractArray{T}, S::Vector{Tr}, Vt::AbstractArray{T}) = new(U, S, Vt) + SVD{T,Tr,M}(U::AbstractArray{T}, S::Vector{Tr}, Vt::AbstractArray{T}) where (T,Tr,M) = + new(U, S, Vt) end -SVD{T,Tr}(U::AbstractArray{T}, S::Vector{Tr}, Vt::AbstractArray{T}) = SVD{T,Tr,typeof(U)}(U, S, Vt) +SVD(U::AbstractArray{T}, S::Vector{Tr}, Vt::AbstractArray{T}) where (T,Tr) = SVD{T,Tr,typeof(U)}(U, S, Vt) """ svdfact!(A, thin::Bool=true) -> SVD @@ -182,9 +183,15 @@ immutable GeneralizedSVD{T,S} <: Factorization{T} k::Int l::Int R::S - GeneralizedSVD(U::AbstractMatrix{T}, V::AbstractMatrix{T}, Q::AbstractMatrix{T}, a::Vector, b::Vector, k::Int, l::Int, R::AbstractMatrix{T}) = new(U, V, Q, a, b, k, l, R) + function GeneralizedSVD{T,S}(U::AbstractMatrix{T}, V::AbstractMatrix{T}, Q::AbstractMatrix{T}, + a::Vector, b::Vector, k::Int, l::Int, R::AbstractMatrix{T}) where (T,S) + new(U, V, Q, a, b, k, l, R) + end +end +function GeneralizedSVD(U::AbstractMatrix{T}, V::AbstractMatrix{T}, Q::AbstractMatrix{T}, + a::Vector, b::Vector, k::Int, l::Int, R::AbstractMatrix{T}) where T + GeneralizedSVD{T,typeof(U)}(U, V, Q, a, b, k, l, R) end -GeneralizedSVD{T}(U::AbstractMatrix{T}, V::AbstractMatrix{T}, Q::AbstractMatrix{T}, a::Vector, b::Vector, k::Int, l::Int, R::AbstractMatrix{T}) = GeneralizedSVD{T,typeof(U)}(U, V, Q, a, b, k, l, R) """ svdfact!(A, B) -> GeneralizedSVD diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index 26c2e7fe49b6e..47070bf167a0d 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -6,7 +6,7 @@ immutable SymTridiagonal{T} <: AbstractMatrix{T} dv::Vector{T} # diagonal ev::Vector{T} # subdiagonal - function SymTridiagonal(dv::Vector{T}, ev::Vector{T}) + function SymTridiagonal{T}(dv::Vector{T}, ev::Vector{T}) where T if !(length(dv) - 1 <= length(ev) <= length(dv)) throw(DimensionMismatch("subdiagonal has wrong length. Has length $(length(ev)), but should be either $(length(dv) - 1) or $(length(dv)).")) end @@ -46,9 +46,9 @@ julia> SymTridiagonal(dv, ev) ⋅ ⋅ 9 4 ``` """ -SymTridiagonal{T}(dv::Vector{T}, ev::Vector{T}) = SymTridiagonal{T}(dv, ev) +SymTridiagonal(dv::Vector{T}, ev::Vector{T}) where T = SymTridiagonal{T}(dv, ev) -function SymTridiagonal{Td,Te}(dv::AbstractVector{Td}, ev::AbstractVector{Te}) +function SymTridiagonal(dv::AbstractVector{Td}, ev::AbstractVector{Te}) where (Td,Te) T = promote_type(Td,Te) SymTridiagonal(convert(Vector{T}, dv), convert(Vector{T}, ev)) end diff --git a/base/multi.jl b/base/multi.jl index 2f4d676a4f20c..d0b52807c4e31 100644 --- a/base/multi.jl +++ b/base/multi.jl @@ -710,7 +710,10 @@ type RemoteChannel{T<:AbstractChannel} <: AbstractRemoteRef whence::Int id::Int - RemoteChannel(w::Int, rrid::RRID) = (r = new(w, rrid.whence, rrid.id); return test_existing_ref(r)) + function RemoteChannel{T}(w::Int, rrid::RRID) where T<:AbstractChannel + r = new(w, rrid.whence, rrid.id) + return test_existing_ref(r) + end end function test_existing_ref(r::AbstractRemoteRef) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index ffe1b10d7a7af..90ecb829ed643 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -15,16 +15,16 @@ module IteratorsMD # CartesianIndex immutable CartesianIndex{N} <: AbstractCartesianIndex{N} I::NTuple{N,Int} - CartesianIndex(index::NTuple{N,Integer}) = new(index) + CartesianIndex{N}(index::NTuple{N,Integer}) where N = new(index) end - CartesianIndex{N}(index::NTuple{N,Integer}) = CartesianIndex{N}(index) - (::Type{CartesianIndex})(index::Integer...) = CartesianIndex(index) - (::Type{CartesianIndex{N}}){N}(index::Vararg{Integer,N}) = CartesianIndex{N}(index) + CartesianIndex(index::NTuple{N,Integer}) where N = CartesianIndex{N}(index) + CartesianIndex(index::Integer...) = CartesianIndex(index) + CartesianIndex{N}(index::Vararg{Integer,N}) where N = CartesianIndex{N}(index) # Allow passing tuples smaller than N - (::Type{CartesianIndex{N}}){N}(index::Tuple) = CartesianIndex{N}(fill_to_length(index, 1, Val{N})) - (::Type{CartesianIndex{N}}){N}(index::Integer...) = CartesianIndex{N}(index) - (::Type{CartesianIndex{N}}){N}() = CartesianIndex{N}(()) + CartesianIndex{N}(index::Tuple) where N = CartesianIndex{N}(fill_to_length(index, 1, Val{N})) + CartesianIndex{N}(index::Integer...) where N = CartesianIndex{N}(index) + CartesianIndex{N}() where N = CartesianIndex{N}(()) # Un-nest passed CartesianIndexes CartesianIndex(index::Union{Integer, CartesianIndex}...) = CartesianIndex(flatten(index)) flatten(I::Tuple{}) = I @@ -276,10 +276,10 @@ wrapped with `LogicalIndex` upon calling `to_indices`. immutable LogicalIndex{T, A<:AbstractArray{Bool}} <: AbstractVector{T} mask::A sum::Int - LogicalIndex(mask::A) = new(mask, countnz(mask)) + LogicalIndex{T,A}(mask::A) where (T,A<:AbstractArray{Bool}) = new(mask, countnz(mask)) end LogicalIndex(mask::AbstractVector{Bool}) = LogicalIndex{Int, typeof(mask)}(mask) -LogicalIndex{N}(mask::AbstractArray{Bool, N}) = LogicalIndex{CartesianIndex{N}, typeof(mask)}(mask) +LogicalIndex(mask::AbstractArray{Bool, N}) where N = LogicalIndex{CartesianIndex{N}, typeof(mask)}(mask) (::Type{LogicalIndex{Int}})(mask::AbstractArray) = LogicalIndex{Int, typeof(mask)}(mask) size(L::LogicalIndex) = (L.sum,) length(L::LogicalIndex) = L.sum diff --git a/base/multinverses.jl b/base/multinverses.jl index c358b360c15e0..c1a72ec84ecde 100644 --- a/base/multinverses.jl +++ b/base/multinverses.jl @@ -49,7 +49,7 @@ immutable SignedMultiplicativeInverse{T<:Signed} <: MultiplicativeInverse{T} addmul::Int8 shift::UInt8 - function SignedMultiplicativeInverse(d::T) + function SignedMultiplicativeInverse{T}(d::T) where T<:Signed d == 0 && throw(ArgumentError("cannot compute magic for d == $d")) signedmin = unsigned(typemin(T)) UT = unsigned(T) @@ -94,7 +94,7 @@ immutable UnsignedMultiplicativeInverse{T<:Unsigned} <: MultiplicativeInverse{T} add::Bool shift::UInt8 - function UnsignedMultiplicativeInverse(d::T) + function UnsignedMultiplicativeInverse{T}(d::T) where T<:Unsigned d == 0 && throw(ArgumentError("cannot compute magic for d == $d")) u2 = convert(T, 2) add = false diff --git a/base/permuteddimsarray.jl b/base/permuteddimsarray.jl index 29c71822c6a89..b3d4baa3e907a 100644 --- a/base/permuteddimsarray.jl +++ b/base/permuteddimsarray.jl @@ -8,7 +8,7 @@ export permutedims immutable PermutedDimsArray{T,N,perm,iperm,AA<:AbstractArray} <: AbstractArray{T,N} parent::AA - function PermutedDimsArray(data::AA) + function PermutedDimsArray{T,N,perm,iperm,AA}(data::AA) where (T,N,perm,iperm,AA<:AbstractArray) (isa(perm, NTuple{N,Int}) && isa(iperm, NTuple{N,Int})) || error("perm and iperm must both be NTuple{$N,Int}") isperm(perm) || throw(ArgumentError(string(perm, " is not a valid permutation of dimensions 1:", N))) all(map(d->iperm[perm[d]]==d, 1:N)) || throw(ArgumentError(string(perm, " and ", iperm, " must be inverses"))) @@ -16,7 +16,7 @@ immutable PermutedDimsArray{T,N,perm,iperm,AA<:AbstractArray} <: AbstractArray{T end end -function PermutedDimsArray{T,N}(data::AbstractArray{T,N}, perm) +function PermutedDimsArray(data::AbstractArray{T,N}, perm) where (T,N) length(perm) == N || throw(ArgumentError(string(perm, " is not a valid permutation of dimensions 1:", N))) iperm = invperm(perm) PermutedDimsArray{T,N,(perm...,),(iperm...,),typeof(data)}(data) diff --git a/base/refpointer.jl b/base/refpointer.jl index 45f75752091a5..100084a535907 100644 --- a/base/refpointer.jl +++ b/base/refpointer.jl @@ -37,8 +37,8 @@ unsafe_convert{T}(::Type{Ref{T}}, x) = unsafe_convert(Ptr{T}, x) type RefValue{T} <: Ref{T} x::T - RefValue() = new() - RefValue(x) = new(x) + RefValue{T}() where T = new() + RefValue{T}(x) where T = new(x) end RefValue{T}(x::T) = RefValue{T}(x) isassigned(x::RefValue) = isdefined(x, :x) @@ -64,11 +64,11 @@ end unsafe_convert{T}(::Type{Ptr{Void}}, b::RefValue{T}) = convert(Ptr{Void}, unsafe_convert(Ptr{T}, b)) ### Methods for a Ref object that is backed by an array at index i -immutable RefArray{T, A<:AbstractArray, R} <: Ref{T} +immutable RefArray{T, A<:AbstractArray{T}, R} <: Ref{T} x::A i::Int roots::R # should be either ::Void or ::Any - RefArray(x,i,roots=nothing) = (@assert(eltype(A) == T); new(x,i,roots)) + RefArray{T,A,R}(x,i,roots=nothing) where (T,A<:AbstractArray{T},R) = new(x,i,roots) end RefArray{T}(x::AbstractArray{T},i::Int,roots::Any) = RefArray{T,typeof(x),Any}(x, i, roots) RefArray{T}(x::AbstractArray{T},i::Int=1,roots::Void=nothing) = RefArray{T,typeof(x),Void}(x, i, nothing) diff --git a/base/serialize.jl b/base/serialize.jl index 2defd0c591430..ee40289720394 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -12,7 +12,7 @@ type SerializationState{I<:IO} <: AbstractSerializer io::I counter::Int table::ObjectIdDict - SerializationState(io::I) = new(io, 0, ObjectIdDict()) + SerializationState{I}(io::I) where I<:IO = new(io, 0, ObjectIdDict()) end SerializationState(io::IO) = SerializationState{typeof(io)}(io) diff --git a/base/set.jl b/base/set.jl index 2e6fc6dd5b8ff..7ea74d5a68823 100644 --- a/base/set.jl +++ b/base/set.jl @@ -3,8 +3,8 @@ type Set{T} <: AbstractSet{T} dict::Dict{T,Void} - Set() = new(Dict{T,Void}()) - Set(itr) = union!(new(Dict{T,Void}()), itr) + Set{T}() where T = new(Dict{T,Void}()) + Set{T}(itr) where T = union!(new(Dict{T,Void}()), itr) end Set() = Set{Any}() Set(itr) = Set{eltype(itr)}(itr) diff --git a/base/sharedarray.jl b/base/sharedarray.jl index e0a0f94c57ccf..6ce14d5561433 100644 --- a/base/sharedarray.jl +++ b/base/sharedarray.jl @@ -23,7 +23,7 @@ type SharedArray{T,N} <: DenseArray{T,N} # a subset of workers. loc_subarr_1d::SubArray{T,1,Array{T,1},Tuple{UnitRange{Int}},true} - function SharedArray(d,p,r,sn,s) + function SharedArray{T,N}(d,p,r,sn,s) where (T,N) new(d,p,r,sn,s,0,view(Array{T}(ntuple(d->0,N)), 1:0)) end end diff --git a/base/show.jl b/base/show.jl index febd1b51f7237..3b769ecb8992c 100644 --- a/base/show.jl +++ b/base/show.jl @@ -13,7 +13,8 @@ dictionary operations such as [`getindex`](@ref), and can also be used as an I/O immutable IOContext{IO_t <: IO} <: AbstractPipe io::IO_t dict::ImmutableDict{Symbol, Any} - function IOContext(io::IO_t, dict::ImmutableDict{Symbol, Any}) + + function IOContext{IO_t}(io::IO_t, dict::ImmutableDict{Symbol, Any}) where IO_t<:IO assert(!(IO_t <: IOContext)) return new(io, dict) end diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index cec3776140e7c..abb8c5ba1e2b5 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -238,7 +238,7 @@ end type Sparse{Tv<:VTypes} <: AbstractSparseMatrix{Tv,SuiteSparse_long} p::Ptr{C_Sparse{Tv}} - function Sparse(p::Ptr{C_Sparse{Tv}}) + function Sparse{Tv}(p::Ptr{C_Sparse{Tv}}) where Tv<:VTypes if p == C_NULL throw(ArgumentError("sparse matrix construction failed for " * "unknown reasons. Please submit a bug report.")) @@ -246,7 +246,7 @@ type Sparse{Tv<:VTypes} <: AbstractSparseMatrix{Tv,SuiteSparse_long} new(p) end end -Sparse{Tv<:VTypes}(p::Ptr{C_Sparse{Tv}}) = Sparse{Tv}(p) +Sparse(p::Ptr{C_Sparse{Tv}}) where Tv<:VTypes = Sparse{Tv}(p) # Factor @@ -317,7 +317,7 @@ end type Factor{Tv} <: Factorization{Tv} p::Ptr{C_Factor{Tv}} - function Factor(p::Ptr{C_Factor{Tv}}) + function Factor{Tv}(p::Ptr{C_Factor{Tv}}) where Tv if p == C_NULL throw(ArgumentError("factorization construction failed for " * "unknown reasons. Please submit a bug report.")) @@ -325,7 +325,7 @@ type Factor{Tv} <: Factorization{Tv} new(p) end end -Factor{Tv<:VTypes}(p::Ptr{C_Factor{Tv}}) = Factor{Tv}(p) +Factor(p::Ptr{C_Factor{Tv}}) where Tv<:VTypes = Factor{Tv}(p) # Define get similar to get(Nullable) to check pointers. All pointer loads # should be wrapped in get to make sure that SuiteSparse is not called with @@ -344,7 +344,7 @@ end type FactorComponent{Tv,S} <: AbstractMatrix{Tv} F::Factor{Tv} - function FactorComponent(F::Factor{Tv}) + function FactorComponent{Tv,S}(F::Factor{Tv}) where (Tv,S) s = unsafe_load(get(F.p)) if s.is_ll != 0 if !(S == :L || S == :U || S == :PtL || S == :UP) @@ -359,7 +359,7 @@ type FactorComponent{Tv,S} <: AbstractMatrix{Tv} new(F) end end -function FactorComponent{Tv}(F::Factor{Tv}, sym::Symbol) +function FactorComponent(F::Factor{Tv}, sym::Symbol) where Tv FactorComponent{Tv,sym}(F) end diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 8732875eee35f..b570ebd0c7b40 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -12,7 +12,8 @@ immutable SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti} rowval::Vector{Ti} # Row values of nonzeros nzval::Vector{Tv} # Nonzero values - function SparseMatrixCSC(m::Integer, n::Integer, colptr::Vector{Ti}, rowval::Vector{Ti}, nzval::Vector{Tv}) + function SparseMatrixCSC{Tv,Ti}(m::Integer, n::Integer, colptr::Vector{Ti}, rowval::Vector{Ti}, + nzval::Vector{Tv}) where (Tv,Ti<:Integer) m < 0 && throw(ArgumentError("number of rows (m) must be ≥ 0, got $m")) n < 0 && throw(ArgumentError("number of columns (n) must be ≥ 0, got $n")) new(Int(m), Int(n), colptr, rowval, nzval) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index ef94ec75c4006..81efd1627e394 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -14,7 +14,7 @@ immutable SparseVector{Tv,Ti<:Integer} <: AbstractSparseVector{Tv,Ti} nzind::Vector{Ti} # the indices of nonzeros nzval::Vector{Tv} # the values of nonzeros - function SparseVector(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) + function SparseVector{Tv,Ti}(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) where (Tv,Ti<:Integer) n >= 0 || throw(ArgumentError("The number of elements must be non-negative.")) length(nzind) == length(nzval) || throw(ArgumentError("index and value vectors must be the same length")) @@ -22,7 +22,7 @@ immutable SparseVector{Tv,Ti<:Integer} <: AbstractSparseVector{Tv,Ti} end end -SparseVector{Tv,Ti}(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) = +SparseVector(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) where (Tv,Ti) = SparseVector{Tv,Ti}(n, nzind, nzval) ### Basic properties diff --git a/base/sparse/spqr.jl b/base/sparse/spqr.jl index 6f93e2049426c..435a87f1dd195 100644 --- a/base/sparse/spqr.jl +++ b/base/sparse/spqr.jl @@ -54,14 +54,14 @@ type Factorization{Tv<:VTypes} <: Base.LinAlg.Factorization{Tv} m::Int n::Int p::Ptr{C_Factorization{Tv}} - function Factorization(m::Integer, n::Integer, p::Ptr{C_Factorization{Tv}}) + function Factorization{Tv}(m::Integer, n::Integer, p::Ptr{C_Factorization{Tv}}) where Tv<:VTypes if p == C_NULL throw(ArgumentError("factorization failed for unknown reasons. Please submit a bug report.")) end new(m, n, p) end end -Factorization{Tv<:VTypes}(m::Integer, n::Integer, p::Ptr{C_Factorization{Tv}}) = Factorization{Tv}(m, n, p) +Factorization(m::Integer, n::Integer, p::Ptr{C_Factorization{Tv}}) where Tv<:VTypes = Factorization{Tv}(m, n, p) size(F::Factorization) = (F.m, F.n) function size(F::Factorization, i::Integer) diff --git a/base/subarray.jl b/base/subarray.jl index a8cf520e21a7a..57fc5855633dc 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -10,7 +10,7 @@ immutable SubArray{T,N,P,I,L} <: AbstractArray{T,N} indexes::I offset1::Int # for linear indexing and pointer, only valid when L==true stride1::Int # used only for linear indexing - function SubArray(parent, indexes, offset1, stride1) + function SubArray{T,N,P,I,L}(parent, indexes, offset1, stride1) where (T,N,P,I,L) @_inline_meta check_parent_index_match(parent, indexes) new(parent, indexes, offset1, stride1) @@ -21,11 +21,11 @@ function SubArray(parent::AbstractArray, indexes::Tuple) @_inline_meta SubArray(linearindexing(viewindexing(indexes), linearindexing(parent)), parent, ensure_indexable(indexes), index_dimsum(indexes...)) end -function SubArray{P, I, N}(::LinearSlow, parent::P, indexes::I, ::NTuple{N,Any}) +function SubArray(::LinearSlow, parent::P, indexes::I, ::NTuple{N,Any}) where (P,I,N) @_inline_meta SubArray{eltype(P), N, P, I, false}(parent, indexes, 0, 0) end -function SubArray{P, I, N}(::LinearFast, parent::P, indexes::I, ::NTuple{N,Any}) +function SubArray(::LinearFast, parent::P, indexes::I, ::NTuple{N,Any}) where (P,I,N) @_inline_meta # Compute the stride and offset stride1 = compute_stride1(parent, indexes) diff --git a/base/weakkeydict.jl b/base/weakkeydict.jl index cd6128dea8d34..ea28c9805f0ba 100644 --- a/base/weakkeydict.jl +++ b/base/weakkeydict.jl @@ -15,7 +15,7 @@ type WeakKeyDict{K,V} <: Associative{K,V} finalizer::Function # Constructors mirror Dict's - function WeakKeyDict() + function WeakKeyDict{K,V}() where V where K t = new(Dict{Any,V}(), Threads.RecursiveSpinLock(), identity) t.finalizer = function (k) # when a weak key is finalized, remove from dictionary if it is still there @@ -24,22 +24,22 @@ type WeakKeyDict{K,V} <: Associative{K,V} end return t end - function WeakKeyDict(kv) - h = WeakKeyDict{K,V}() - for (k,v) in kv - h[k] = v - end - return h +end +function WeakKeyDict{K,V}(kv) where V where K + h = WeakKeyDict{K,V}() + for (k,v) in kv + h[k] = v end - WeakKeyDict(p::Pair) = setindex!(WeakKeyDict{K,V}(), p.second, p.first) - function WeakKeyDict(ps::Pair...) - h = WeakKeyDict{K,V}() - sizehint!(h, length(ps)) - for p in ps - h[p.first] = p.second - end - return h + return h +end +WeakKeyDict{K,V}(p::Pair) where V where K = setindex!(WeakKeyDict{K,V}(), p.second, p.first) +function WeakKeyDict{K,V}(ps::Pair...) where V where K + h = WeakKeyDict{K,V}() + sizehint!(h, length(ps)) + for p in ps + h[p.first] = p.second end + return h end WeakKeyDict() = WeakKeyDict{Any,Any}() diff --git a/doc/src/manual/constructors.md b/doc/src/manual/constructors.md index e360200de2b21..67dd85f6dc79b 100644 --- a/doc/src/manual/constructors.md +++ b/doc/src/manual/constructors.md @@ -334,19 +334,16 @@ This automatic provision of constructors is equivalent to the following explicit julia> type Point{T<:Real} x::T y::T - Point(x,y) = new(x,y) + Point{T}(x,y) where T<:Real = new(x,y) end -julia> Point{T<:Real}(x::T, y::T) = Point{T}(x,y); +julia> Point(x::T, y::T) where T<:Real = Point{T}(x,y); ``` -Some features of parametric constructor definitions at work here deserve comment. First, inner -constructor declarations always define methods of `Point{T}` rather than methods of the general -`Point` constructor function. Since `Point` is not a concrete type, it makes no sense for it to -even have inner constructor methods at all. Thus, the inner method declaration `Point(x,y) = new(x,y)` -provides an inner constructor method for each value of `T`. It is this method declaration that -defines the behavior of constructor calls with explicit type parameters like `Point{Int64}(1,2)` -and `Point{Float64}(1.0,2.0)`. The outer constructor declaration, on the other hand, defines a +Notice that each definition looks like the form of constructor call that it handles. +The call `Point{Int64}(1,2)` will invoke the definition `Point{T}(x,y)` inside the +`type` block. +The outer constructor declaration, on the other hand, defines a method for the general `Point` constructor which only applies to pairs of values of the same real type. This declaration makes constructor calls without explicit type parameters, like `Point(1,2)` and `Point(1.0,2.5)`, work. Since the method declaration restricts the arguments to being of the @@ -384,7 +381,7 @@ Closest candidates are: Point{T<:Real}(::T<:Real, !Matched::T<:Real) at none:1 ``` -For a much more general way of making all such calls work sensibly, see [Conversion and Promotion](@ref conversion-and-promotion). +For a more general way to make all such calls work sensibly, see [Conversion and Promotion](@ref conversion-and-promotion). At the risk of spoiling the suspense, we can reveal here that all it takes is the following outer method definition to make all calls to the general `Point` constructor work as one would expect: @@ -422,7 +419,7 @@ which implements Julia's [Rational Numbers](@ref): julia> immutable OurRational{T<:Integer} <: Real num::T den::T - function OurRational(num::T, den::T) + function OurRational{T}(num::T, den::T) where T<:Integer if num == 0 && den == 0 error("invalid rational: 0//0") end @@ -433,7 +430,7 @@ julia> immutable OurRational{T<:Integer} <: Real end end -julia> OurRational{T<:Integer}(n::T, d::T) = OurRational{T}(n,d) +julia> OurRational(n::T, d::T) where T<:Integer = OurRational{T}(n,d) OurRational julia> OurRational(n::Integer, d::Integer) = OurRational(promote(n,d)...) @@ -533,7 +530,7 @@ one type to another, you should probably define a `convert` method instead. On the other hand, if your constructor does not represent a lossless conversion, or doesn't represent "conversion" at all, it is better to leave it as a constructor rather than a `convert` method. - For example, the `Array{Int}()` constructor creates a zero-dimensional `Array` of the type `Int`, +For example, the `Array{Int}()` constructor creates a zero-dimensional `Array` of the type `Int`, but is not really a "conversion" from `Int` to an `Array`. ## Outer-only constructors @@ -561,14 +558,14 @@ SummedArray{Int32,Int32}(Int32[1,2,3],6) The problem is that we want `S` to be a larger type than `T`, so that we can sum many elements with less information loss. For example, when `T` is `Int32`, we would like `S` to be `Int64`. Therefore we want to avoid an interface that allows the user to construct instances of the type -`SummedArray{Int32,Int32}`. One way to do this is to provide only an outer constructor for `SummedArray`. -This can be done using method definition by type: +`SummedArray{Int32,Int32}`. One way to do this is to provide a constructor only for `SummedArray`, +but inside the `type` definition block to suppress generation of default constructors: ```jldoctest julia> type SummedArray{T<:Number,S<:Number} data::Vector{T} sum::S - function (::Type{SummedArray}){T}(a::Vector{T}) + function SummedArray(a::Vector{T}) where T S = widen(T) new{T,S}(a, sum(S, a)) end @@ -583,4 +580,5 @@ Closest candidates are: This constructor will be invoked by the syntax `SummedArray(a)`. The syntax `new{T,S}` allows specifying parameters for the type to be constructed, i.e. this call will return a `SummedArray{T,S}`. - +`new{T,S}` can be used in any constructor definition, but for convenience the parameters +to `new{}` are automatically derived from the type being constructed when possible. diff --git a/examples/ModInts.jl b/examples/ModInts.jl index 3d4130332e404..0effca60b1c0d 100644 --- a/examples/ModInts.jl +++ b/examples/ModInts.jl @@ -7,7 +7,7 @@ import Base: +, -, *, /, inv immutable ModInt{n} <: Integer k::Int - ModInt(k) = new(mod(k,n)) + ModInt{n}(k) where n = new(mod(k,n)) end Base.show{n}(io::IO, k::ModInt{n}) = diff --git a/examples/lru.jl b/examples/lru.jl index 4458032be03b7..83e34a18a937f 100644 --- a/examples/lru.jl +++ b/examples/lru.jl @@ -39,7 +39,7 @@ type UnboundedLRU{K,V} <: LRU{K,V} ht::Dict q::Vector{CacheItem} - UnboundedLRU() = new(Dict(), similar(Array{CacheItem}(1), 0)) + UnboundedLRU{K,V}() where (K,V) = new(Dict(), similar(Array{CacheItem}(1), 0)) end UnboundedLRU() = UnboundedLRU{Any, Any}() @@ -48,8 +48,8 @@ type BoundedLRU{K,V} <: LRU{K,V} q::Vector{CacheItem} maxsize::Int - BoundedLRU(m) = new(Dict(), similar(Array{CacheItem}(1), 0), m) - BoundedLRU() = BoundedLRU(__MAXCACHE) + BoundedLRU{K,V}(m) where (K,V) = new(Dict(), similar(Array{CacheItem}(1), 0), m) + BoundedLRU{K,V}() where (K,V) = BoundedLRU(__MAXCACHE) end BoundedLRU(m) = BoundedLRU{Any, Any}(m) BoundedLRU() = BoundedLRU{Any, Any}() diff --git a/src/ast.scm b/src/ast.scm index 4c0ae0d0dda70..f48a5583145c0 100644 --- a/src/ast.scm +++ b/src/ast.scm @@ -83,7 +83,10 @@ ((generator) (string "(" (deparse (cadr e)) " for " (deparse-arglist (cddr e) ", ") ")")) ((where) - (string (deparse (cadr e)) " where " (deparse (caddr e)))) + (string (deparse (cadr e)) " where " + (if (length= e 3) + (deparse (caddr e)) + (deparse (cons 'tuple (cddr e)))))) ((function for while) (deparse-block (string (car e) " " (deparse (cadr e))) (block-stmts (caddr e)))) diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index d6a81d8aa014c..196754f842d73 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -767,6 +767,9 @@ (let* ((temp (ctor-signature name params bounds curlyargs sig)) (sig (car temp)) (params (cdr temp))) + (if (pair? params) + (syntax-deprecation #f (string "inner constructor " name "(...)") + (deparse `(where (call (curly ,name ,@params) ...) ,@params)))) `(,keyword ,sig ,(ctor-body body params))))))) ;; rewrite calls to `new( ... )` to `new` expressions on the appropriate diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 9cf12eca837c6..03df99cbc04b1 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -174,15 +174,17 @@ type T24Linear{T,N,dims} <: AbstractArray{T,N} v1::T; v2::T; v3::T; v4::T; v5::T; v6::T; v7::T; v8::T v9::T; v10::T; v11::T; v12::T; v13::T; v14::T; v15::T; v16::T v17::T; v18::T; v19::T; v20::T; v21::T; v22::T; v23::T; v24::T - T24Linear() = (prod(dims) == 24 || throw(DimensionMismatch("T24Linear must have 24 elements")); new()) - function T24Linear(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24) - prod(dims) == 24 || throw(DimensionMismatch("T24Linear must have 24 elements")) + T24Linear{T,N,d}() where (T,N,d) = + (prod(d) == 24 || throw(DimensionMismatch("T24Linear must have 24 elements")); new()) + function T24Linear{T,N,d}(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12, + v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24) where (T,N,d) + prod(d) == 24 || throw(DimensionMismatch("T24Linear must have 24 elements")) new(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24) end end -T24Linear{T}(::Type{T}, dims::Int...) = T24Linear(T, dims) -T24Linear{T,N}(::Type{T}, dims::NTuple{N,Int}) = T24Linear{T,N,dims}() +T24Linear(::Type{T}, dims::Int...) where T = T24Linear(T, dims) +T24Linear(::Type{T}, dims::NTuple{N,Int}) where (T,N) = T24Linear{T,N,dims}() Base.convert{T,N }(::Type{T24Linear }, X::AbstractArray{T,N}) = convert(T24Linear{T,N}, X) Base.convert{T,N,_}(::Type{T24Linear{T }}, X::AbstractArray{_,N}) = convert(T24Linear{T,N}, X) diff --git a/test/core.jl b/test/core.jl index 12673ffaa271c..32a6764ec8545 100644 --- a/test/core.jl +++ b/test/core.jl @@ -1056,7 +1056,7 @@ end # issue #2562 type Node2562{T} value::T - Node2562(value::T) = new(value) + Node2562{T}(value::T) where T = new(value) end Node2562{T}(value::T, args...) = Node2562{T}(value, args...) makenode2562(value) = Node2562(value) @@ -1066,7 +1066,7 @@ makenode2562(value) = Node2562(value) # issue #2619 type I2619{T} v::T - I2619(v) = new(convert(T,v)) + I2619{T}(v) where T = new(convert(T,v)) end bad2619 = false function i2619() @@ -1252,8 +1252,8 @@ convert_default_should_fail_here() = similar([1],typeof(zero(typeof(rand(2,2)))) type Foo4376{T} x - Foo4376(x::T) = new(x) - Foo4376(a::Foo4376{Int}) = new(a.x) + Foo4376{T}(x::T) where T = new(x) + Foo4376{T}(a::Foo4376{Int}) where T = new(a.x) end @test isa(Foo4376{Float32}(Foo4376{Int}(2)), Foo4376{Float32}) @@ -1657,11 +1657,11 @@ end # issue #6404 type type_2{T <: Integer, N} <: Number x::T - type_2(n::T) = new(n) + type_2{T,N}(n::T) where (T<:Integer,N) = new(n) end type type_1{T <: Number} <: Number x::Vector{T} - type_1(x::Vector{T}) = new(x) + type_1{T}(x::Vector{T}) where T<:Number = new(x) end type_1{T <: Number}(x::Vector{T}) = type_1{T}(x) type_1{T <: Number}(c::T) = type_1{T}([c]) @@ -2834,7 +2834,7 @@ end # issue #11675 immutable T11675{T} x::T - T11675() = new() + T11675{T}() where T = new() end let x = T11675{Union{}}() function f11675(x) @@ -2999,7 +2999,7 @@ failure12003(dt=DATE12003) = Dates.year(dt) # issue #12089 type A12089{K, N} sz::NTuple{N, Int} - A12089(sz::NTuple{N, Int}) = new(sz) + A12089{K,N}(sz::NTuple{N, Int}) where (K,N) = new(sz) end A12089{-1, 1}((1,)) diff --git a/test/inference.jl b/test/inference.jl index b13a34d720aa0..fa7a790070774 100644 --- a/test/inference.jl +++ b/test/inference.jl @@ -77,7 +77,7 @@ end immutable Hanoi5906{T} <: Outer5906{T} a::T succ :: Outer5906{Inner5906{T}} - Hanoi5906(a) = new(a, Empty5906{Inner5906{T}}()) + Hanoi5906{T}(a) where T = new(a, Empty5906{Inner5906{T}}()) end function f5906{T}(h::Hanoi5906{T}) diff --git a/test/keywordargs.jl b/test/keywordargs.jl index 0e1057e80155d..61f964c19a891 100644 --- a/test/keywordargs.jl +++ b/test/keywordargs.jl @@ -173,7 +173,7 @@ end # issue #4801 type T4801{X} - T4801(;k=0) = new() + T4801{X}(;k=0) where X = new() end @test isa(T4801{Any}(k=0), T4801{Any}) diff --git a/test/vecelement.jl b/test/vecelement.jl index 10ea4c5d8e899..944118d856516 100644 --- a/test/vecelement.jl +++ b/test/vecelement.jl @@ -40,7 +40,7 @@ b = Bunch((VecElement(1.0), VecElement(2.0))) immutable Herd{N,T} elts::NTuple{N,Base.VecElement{T}} - Herd(elts::NTuple{N,T}) = new(ntuple(i->Base.VecElement{T}(elts[i]), N)) + Herd{N,T}(elts::NTuple{N,T}) where (N,T) = new(ntuple(i->Base.VecElement{T}(elts[i]), N)) end function check{N,T}(x::Herd{N,T}) diff --git a/test/worlds.jl b/test/worlds.jl index 725204b7d730b..d9f54d662ed8b 100644 --- a/test/worlds.jl +++ b/test/worlds.jl @@ -66,7 +66,7 @@ A265(fld::Int) = A265(Float64(fld)) type B265{T} field1::T # dummy arg is present to prevent (::Type{T}){T}(arg) from matching the test calls - B265(field1::Any, dummy::Void) = new(field1) # prevent generation of outer ctor + B265{T}(field1::Any, dummy::Void) where T = new(field1) # prevent generation of outer ctor end # define some constructors B265(x::Int, dummy::Void) = B265{Int}(x, dummy)