Skip to content

Commit 909ea47

Browse files
authored
Merge pull request #117 from aytekinar/patch-1
Revert back `eltype{T}(::Poly{T})`
2 parents 101d574 + 23c875b commit 909ea47

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Polynomials.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export degree, coeffs, variable
1212
export polyval, polyint, polyder, roots, polyfit
1313
export Pade, padeval
1414

15-
import Base: start, next, done, length, size, eltype
15+
import Base: start, next, done, length, size, eltype, collect
1616
import Base: endof, getindex, setindex!, copy, zero, one, convert, norm, gcd
1717
import Base: show, print, *, /, //, -, +, ==, isapprox, divrem, div, rem, eltype
1818
import Base: promote_rule, truncate, chop, conj, transpose, dot, hash
@@ -134,7 +134,8 @@ convert{T, S<:Number}(::Type{Poly{T}}, x::S, var::SymbolLike=:x) = Poly(T[x], va
134134
convert{T, S<:Number}(::Type{Poly{T}}, x::AbstractArray{S}, var::SymbolLike=:x) = map(el->Poly(T[el],var), x)
135135
promote_rule{T, S}(::Type{Poly{T}}, ::Type{Poly{S}}) = Poly{promote_type(T, S)}
136136
promote_rule{T, S<:Number}(::Type{Poly{T}}, ::Type{S}) = Poly{promote_type(T, S)}
137-
eltype{T}(::Poly{T}) = Poly{T}
137+
# Check JuliaLang/METADATA.jl#8528
138+
eltype{T}(::Poly{T}) = T
138139

139140
length(p::Poly) = length(coeffs(p))
140141
endof(p::Poly) = length(p) - 1
@@ -144,6 +145,9 @@ next(p::Poly, state) = (temp = zeros(coeffs(p)); temp[state+1] = p[state]; (Pol
144145
done(p::Poly, state) = state > degree(p)
145146
eltype{T}(::Type{Poly{T}}) = Poly{T}
146147

148+
# shortcut for collect(eltype, collection)
149+
collect{T}(p::Poly{T}) = collect(Poly{T}, p)
150+
147151
size(p::Poly) = size(p.a)
148152
size(p::Poly, i::Integer) = size(p.a, i)
149153

test/runtests.jl

+12
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,16 @@ for term in p1
326326
end
327327

328328
@test eltype(typeof(p1)) == typeof(p1)
329+
330+
# after implementing the iteration interface, i.e., `start` and its friends,
331+
# previously throwing `collect` function (and maybe, other similar functions)
332+
# started giving different errors due to `eltype{T}(::Poly{T}) = T`. Changing
333+
# this definition has resulted in JuliaLang/METADATA.jl#8528. One small fix
334+
# was to direct `collect{T}(p::Poly{T})` to `collect(Poly{T}, p)`.
335+
336+
@test eltype(p1) == Int
337+
@test eltype(collect(p1)) == Poly{Int}
338+
@test eltype(collect(Poly{Float64}, p1)) == Poly{Float64}
339+
@test_throws InexactError collect(Poly{Int}, Poly([1.2]))
340+
329341
@test length(collect(p1)) == degree(p1)+1

0 commit comments

Comments
 (0)