Skip to content

Commit

Permalink
make core eltype() definitions operate on types, so it is not necessary
Browse files Browse the repository at this point in the history
to define it for both types and values

ref discussion in #9281
  • Loading branch information
JeffBezanson committed Dec 9, 2014
1 parent c0adc23 commit 41b2154
Show file tree
Hide file tree
Showing 17 changed files with 26 additions and 31 deletions.
3 changes: 0 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ typealias AbstractVecOrMat{T} Union(AbstractVector{T}, AbstractMatrix{T})

size{T,n}(t::AbstractArray{T,n}, d) = d <= n ? size(t)[d] : 1
size(x, d1::Integer, d2::Integer, dx::Integer...) = tuple(size(x, d1), size(x, d2, dx...)...)
eltype(x) = Any
eltype{T,n}(::AbstractArray{T,n}) = T
eltype{T,n}(::Type{AbstractArray{T,n}}) = T
eltype{T<:AbstractArray}(::Type{T}) = eltype(super(T))
iseltype(x,T) = eltype(x) <: T
elsize{T}(::AbstractArray{T}) = sizeof(T)
isinteger(x::AbstractArray) = all(isinteger,x)
Expand Down
14 changes: 5 additions & 9 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ immutable Combinations{T}
t::Int
end

eltype(c::Combinations) = typeof(c.a)
eltype{T}(c::Combinations{UnitRange{T}}) = Array{T,1}
eltype{T}(c::Combinations{Range{T}}) = Array{T,1}
eltype(c::Combinations) = typeof(c.a[1:c.t])

length(c::Combinations) = binomial(length(c.a),c.t)

Expand Down Expand Up @@ -282,22 +280,20 @@ immutable Permutations{T}
a::T
end

eltype(c::Permutations) = typeof(c.a)
eltype{T}(c::Permutations{UnitRange{T}}) = Array{T,1}
eltype{T}(c::Permutations{Range{T}}) = Array{T,1}
eltype(p::Permutations) = typeof(p.a[1:length(p.a)])

length(c::Permutations) = factorial(length(c.a))
length(p::Permutations) = factorial(length(p.a))

permutations(a) = Permutations(a)

start(p::Permutations) = [1:length(p.a)]
function next(p::Permutations, s)
perm = p.a[s]
if length(p.a) == 0
# special case to generate 1 result for len==0
return (p.a,[1])
return (perm,[1])
end
s = copy(s)
perm = p.a[s]
k = length(s)-1
while k > 0 && s[k] > s[k+1]; k -= 1; end
if k == 0
Expand Down
6 changes: 3 additions & 3 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ end

length(v::Union(KeyIterator,ValueIterator)) = length(v.dict)
isempty(v::Union(KeyIterator,ValueIterator)) = isempty(v.dict)
eltype(v::KeyIterator) = eltype(v.dict)[1]
eltype(v::ValueIterator) = eltype(v.dict)[2]
eltype{D}(::Type{KeyIterator{D}}) = eltype(D)[1]
eltype{D}(::Type{ValueIterator{D}}) = eltype(D)[2]

start(v::Union(KeyIterator,ValueIterator)) = start(v.dict)
done(v::Union(KeyIterator,ValueIterator), state) = done(v.dict, state)
Expand Down Expand Up @@ -201,7 +201,7 @@ function filter!(f::Function, d::Associative)
end
filter(f::Function, d::Associative) = filter!(f,copy(d))

eltype{K,V}(a::Associative{K,V}) = (K,V)
eltype{K,V}(::Type{Associative{K,V}}) = (K,V)

function isequal(l::Associative, r::Associative)
if isa(l,ObjectIdDict) != isa(r,ObjectIdDict)
Expand Down
2 changes: 1 addition & 1 deletion base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ similar(s::IntSet) = IntSet()

copy(s::IntSet) = union!(IntSet(), s)

eltype(s::IntSet) = Int64
eltype(::Type{IntSet}) = Int64

function sizehint!(s::IntSet, top::Integer)
if top >= s.limit
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ function done(itr::EachLine, nada)
true
end
next(itr::EachLine, nada) = (readline(itr.stream), nothing)
eltype(itr::EachLine) = ByteString
eltype(::Type{EachLine}) = ByteString

readlines(s=STDIN) = collect(eachline(s))

Expand Down
1 change: 1 addition & 0 deletions base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ end
done(e::Enumerate, state) = done(e.itr, state[2])

eltype(e::Enumerate) = (Int, eltype(e.itr))
eltype{I}(::Type{Enumerate{I}}) = (Int, eltype(I))

# zip

Expand Down
8 changes: 4 additions & 4 deletions base/linalg/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ type CholmodTriplet{Tv<:CHMVTypes,Ti<:CHMITypes}
x::Vector{Tv}
end

eltype{T<:CHMVTypes}(A::CholmodDense{T}) = T
eltype{T<:CHMVTypes}(A::CholmodFactor{T}) = T
eltype{T<:CHMVTypes}(A::CholmodSparse{T}) = T
eltype{T<:CHMVTypes}(A::CholmodTriplet{T}) = T
eltype{T<:CHMVTypes}(::Type{CholmodDense{T}}) = T
eltype{T<:CHMVTypes}(::Type{CholmodFactor{T}}) = T
eltype{T<:CHMVTypes}(::Type{CholmodSparse{T}}) = T
eltype{T<:CHMVTypes}(::Type{CholmodTriplet{T}}) = T

## The CholmodDense! constructor does not copy the contents, which is generally what you
## want as most uses of CholmodDense objects are read-only.
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

abstract Factorization{T}

eltype{T}(F::Factorization{T}) = T
eltype{T}(::Type{Factorization{T}}) = T

macro assertposdef(A, info)
:(($info)==0 ? $A : throw(PosDefException($info)))
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end

const I = UniformScaling(1)

eltype{T}(J::UniformScaling{T}) = T
eltype{T}(::Type{UniformScaling{T}}) = T
ndims(J::UniformScaling) = 2
getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ))

Expand Down
2 changes: 0 additions & 2 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Nullable{T}(value::T) = Nullable{T}(value)

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

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

function convert{S, T}(::Type{Nullable{T}}, x::Nullable{S})
return isnull(x) ? Nullable{T}() : Nullable(convert(T, get(x)))
end
Expand Down
1 change: 0 additions & 1 deletion base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ isinteger(x::Integer) = true

size(x::Number) = ()
size(x::Number,d) = convert(Int,d)<1 ? throw(BoundsError()) : 1
eltype(x::Number) = typeof(x)
eltype{T<:Number}(::Type{T}) = T
ndims(x::Number) = 0
ndims{T<:Number}(::Type{T}) = 0
Expand Down
5 changes: 5 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ widen{T<:Number}(x::T) = convert(widen(T), x)

sizeof(x) = Core.sizeof(x)

eltype(::Type) = Any
eltype(::Type{Any}) = Any
eltype(t::DataType) = eltype(super(t))
eltype(x) = eltype(typeof(x))

# copying immutable things
copy(x::Union(Symbol,Number,AbstractString,Function,Tuple,LambdaStaticData,
TopNode,QuoteNode,DataType,UnionType)) = x
Expand Down
2 changes: 1 addition & 1 deletion base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pointer_from_objref(x::Any) = ccall(:jl_value_ptr, Ptr{Void}, (Any,), x)
integer(x::Ptr) = convert(UInt, x)
unsigned(x::Ptr) = convert(UInt, x)

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

## limited pointer arithmetic & comparison ##

Expand Down
2 changes: 1 addition & 1 deletion base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ immutable RegexMatchIterator
end
end
compile(itr::RegexMatchIterator) = (compile(itr.regex); itr)
eltype(itr::RegexMatchIterator) = RegexMatch
eltype(::Type{RegexMatchIterator}) = RegexMatch
start(itr::RegexMatchIterator) = match(itr.regex, itr.string, 1, uint32(0))
done(itr::RegexMatchIterator, prev_match) = (prev_match == nothing)

Expand Down
2 changes: 1 addition & 1 deletion base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end

isempty(s::Set) = isempty(s.dict)
length(s::Set) = length(s.dict)
eltype{T}(s::Set{T}) = T
eltype{T}(::Type{Set{T}}) = T

in(x, s::Set) = haskey(s.dict, x)

Expand Down
1 change: 0 additions & 1 deletion base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ show(io::IO, s::AbstractString) = print_quoted(io, s)

sizeof(s::AbstractString) = error("type $(typeof(s)) has no canonical binary representation")

eltype(::AbstractString) = Char
eltype{T<:AbstractString}(::Type{T}) = Char

(*)(s::AbstractString...) = string(s...)
Expand Down
2 changes: 1 addition & 1 deletion base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ indexed_next(I, i, state) = done(I,state) ? throw(BoundsError()) : next(I, state

# eltype

eltype{T}(x::(T...)) = T
eltype{T,_}(::Type{NTuple{_,T}}) = T

## mapping ##

Expand Down

1 comment on commit 41b2154

@iamed2
Copy link
Contributor

@iamed2 iamed2 commented on 41b2154 Dec 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of asking for eltype(x) = eltype(typeof(x)) instead of eltype(x) = Any after writing some stuff in the REPL that confused me but I thought it was too trivial to bring up. Thanks for this!

Please sign in to comment.