Skip to content

Commit 45f91ff

Browse files
authored
assert in conversions (#161)
1 parent b567b2e commit 45f91ff

34 files changed

+107
-108
lines changed

src/ApproxFunBase.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ else
102102
import Base: oneto
103103
end
104104

105+
# assert that the conversion succeeds. This helps with inference as well as sanity
106+
strictconvert(T::Type, x) = convert(T, x)::T
105107

106108
include("LinearAlgebra/LinearAlgebra.jl")
107109
include("Fun.jl")

src/Domains/Grids.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
struct FourierGrid{T} <: AbstractVector{T}
55
n::Int
66
π_over_n::T
7-
FourierGrid{T}(n::Int) where T = new{T}(n, convert(T,π)/n)
7+
FourierGrid{T}(n::Int) where T = new{T}(n, strictconvert(T,π)/n)
88
end
99

1010
size(g::FourierGrid) = (g.n,)
@@ -26,4 +26,4 @@ fp(n) = collect(FourierGrid{Float64}(n))
2626

2727
n=10
2828
T=Float64
29-
convert(T,π)*(0:2:2n-2)/n - [convert(T,π)*(2k-2)/n for k=1:n]
29+
strictconvert(T,π)*(0:2:2n-2)/n - [strictconvert(T,π)*(2k-2)/n for k=1:n]

src/Domains/Segment.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ convert(::Type{Domain{T}}, d::Segment) where {T<:Number} = Segment{T}(leftendpoi
3232
convert(::Type{Domain{T}}, d::Segment) where {T<:SVector} = Segment{T}(leftendpoint(d),rightendpoint(d))
3333
convert(::Type{Segment{T}}, d::Segment) where {T<:Number} = Segment{T}(leftendpoint(d),rightendpoint(d))
3434
convert(::Type{Segment}, d::AbstractInterval) = Segment(leftendpoint(d), rightendpoint(d))
35-
convert(::Type{Segment{T}}, d::AbstractInterval) where T =convert(Segment{T}, convert(Segment, d))
35+
convert(::Type{Segment{T}}, d::AbstractInterval) where T =strictconvert(Segment{T}, strictconvert(Segment, d))
3636

3737

3838

39-
Segment(d::AbstractInterval) = convert(Segment, d)
39+
Segment(d::AbstractInterval) = strictconvert(Segment, d)
4040

4141

4242

@@ -48,7 +48,7 @@ convert(::Type{Segment},::AnyDomain) = AnySegment()
4848
convert(::Type{Interval}, d::Segment{<:Real}) = d.a < d.b ? d.a .. d.b : d.b .. d.a
4949
convert(::Type{ClosedInterval}, ::AnyDomain) = NaN..NaN
5050
convert(::Type{ClosedInterval{T}}, ::AnyDomain) where T = T(NaN)..T(NaN)
51-
Interval(d::Segment) = convert(Interval, d)
51+
Interval(d::Segment) = strictconvert(Interval, d)
5252

5353

5454
## Information

src/Fun.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ coefficient(f::Fun,::Colon) = coefficient(f,1:dimension(space(f)))
7676

7777

7878
convert(::Type{Fun{S,T,VT}},f::Fun{S}) where {T,S,VT} =
79-
Fun{S,T,VT}(f.space, convert(VT,f.coefficients)::VT)
79+
Fun{S,T,VT}(f.space, strictconvert(VT,f.coefficients))
8080
function convert(::Type{Fun{S,T,VT}},f::Fun) where {T,S,VT}
81-
g = Fun(Fun(f.space, convert(VT,f.coefficients)::VT), convert(S,space(f))::S)
81+
g = Fun(Fun(f.space, strictconvert(VT,f.coefficients)), strictconvert(S,space(f)))
8282
Fun{S,T,VT}(g.space, g.coefficients)
8383
end
8484

8585
function convert(::Type{Fun{S,T}},f::Fun{S}) where {T,S}
86-
coeff = convert(AbstractVector{T},f.coefficients)::AbstractVector{T}
86+
coeff = strictconvert(AbstractVector{T},f.coefficients)
8787
Fun{S, T, typeof(coeff)}(f.space, coeff)
8888
end
8989

@@ -92,11 +92,11 @@ convert(::Type{VFun{S,T}},x::Number) where {T,S} =
9292
(x==0 ? zeros(T,S(AnyDomain())) : x*ones(T,S(AnyDomain())))::VFun{S,T}
9393
convert(::Type{Fun{S}},x::Number) where {S} =
9494
(x==0 ? zeros(S(AnyDomain())) : x*ones(S(AnyDomain())))::Fun{S}
95-
convert(::Type{IF},x::Number) where {IF<:Fun} = convert(IF,Fun(x))::IF
95+
convert(::Type{IF},x::Number) where {IF<:Fun} = strictconvert(IF,Fun(x))
9696

97-
Fun{S,T,VT}(f::Fun) where {S,T,VT} = convert(Fun{S,T,VT}, f)
98-
Fun{S,T}(f::Fun) where {S,T} = convert(Fun{S,T}, f)
99-
Fun{S}(f::Fun) where {S} = convert(Fun{S}, f)
97+
Fun{S,T,VT}(f::Fun) where {S,T,VT} = strictconvert(Fun{S,T,VT}, f)
98+
Fun{S,T}(f::Fun) where {S,T} = strictconvert(Fun{S,T}, f)
99+
Fun{S}(f::Fun) where {S} = strictconvert(Fun{S}, f)
100100

101101
# if we are promoting, we need to change to a VFun
102102
Base.promote_rule(::Type{Fun{S,T,VT1}},::Type{Fun{S,V,VT2}}) where {T,V,S,VT1,VT2} =

src/LinearAlgebra/RaggedMatrix.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ end
101101

102102
convert(::Type{RaggedMatrix}, B::AbstractMatrix) = RaggedMatrix{eltype(B)}(B)
103103

104-
RaggedMatrix(B::AbstractMatrix) = convert(RaggedMatrix, B)
105-
RaggedMatrix{T}(B::AbstractMatrix) where T = convert(RaggedMatrix{T}, B)
104+
RaggedMatrix(B::AbstractMatrix) = strictconvert(RaggedMatrix, B)
105+
RaggedMatrix{T}(B::AbstractMatrix) where T = strictconvert(RaggedMatrix{T}, B)
106106

107107
Base.similar(B::RaggedMatrix,::Type{T}) where {T} = RaggedMatrix(Vector{T}(length(B.data)),copy(B.cols),B.m)
108108

src/LinearAlgebra/helper.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ end
106106

107107

108108
scal!(n::Integer,cst::BlasFloat,ret::DenseArray{T},k::Integer) where {T<:BlasFloat} =
109-
BLAS.scal!(n,convert(T,cst),ret,k)
109+
BLAS.scal!(n,strictconvert(T,cst),ret,k)
110110

111111
function scal!(n::Integer,cst::Number,ret::AbstractArray,k::Integer)
112112
@assert k*n length(ret)
@@ -162,8 +162,6 @@ const alternatesign! = negateeven!
162162

163163
alternatesign(v::AbstractVector) = alternatesign!(copy(v))
164164

165-
alternatingvector(n::Integer) = 2*mod([1:n],2) .- 1
166-
167165
function alternatingsum(v::AbstractVector)
168166
ret = zero(eltype(v))
169167
s = 1
@@ -664,7 +662,7 @@ conv(x::AbstractVector, y::AbstractVector) = DSP.conv(x, y)
664662
@generated function conv(x::SVector{N}, y::SVector{M}) where {N,M}
665663
NM = N+M-1
666664
quote
667-
convert(SVector{$NM}, DSP.conv(Vector(x), Vector(y)))
665+
strictconvert(SVector{$NM}, DSP.conv(Vector(x), Vector(y)))
668666
end
669667
end
670668

src/LinearAlgebra/rowvector.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ end
4040

4141
# Conversion of underlying storage
4242
convert(::Type{RowVector{T,V}}, rowvec::RowVector) where {T,V<:AbstractVector} =
43-
RowVector{T,V}(convert(V,rowvec.vec))
43+
RowVector{T,V}(strictconvert(V,rowvec.vec))
4444

4545
# similar tries to maintain the RowVector wrapper and the parent type
4646
@inline similar(rowvec::RowVector) = RowVector(similar(parent(rowvec)))

src/Multivariate/LowRankFun.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ LowRankFun(A::Vector{VFun{S,T}},B::Vector{VFun{M,T}},space::SS) where {S,M,SS,T}
2727
LowRankFun(A::Vector{VFun{S,T}},B::Vector{VFun{M,T}}) where {S,M,T} =
2828
LowRankFun(A,B,space(first(A))space(first(B)))
2929
LowRankFun(A::Vector{VFun{S,T}},B::Vector{VFun{M,V}}) where {S,M,T,V} =
30-
LowRankFun(convert(Vector{VFun{S,promote_type(T,V)}},A),
31-
convert(Vector{VFun{M,promote_type(T,V)}},B),
30+
LowRankFun(strictconvert(Vector{VFun{S,promote_type(T,V)}},A),
31+
strictconvert(Vector{VFun{M,promote_type(T,V)}},B),
3232
space(first(A))space(first(B)))
3333
rank(f::LowRankFun) = length(f.A)
3434
size(f::LowRankFun,k::Integer) = k==1 ? mapreduce(length,max,f.A) : mapreduce(length,max,f.B)

src/Multivariate/VectorFun.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
convert(::Type{Array}, f::ArrayFun) = reshape(vec(f), size(space(f))...)
5-
Array(f::ArrayFun) = convert(Array, f)
5+
Array(f::ArrayFun) = strictconvert(Array, f)
66
Vector(f::VectorFun) = Array(f)
77
Matrix(f::MatrixFun) = Array(f)
88

src/Operators/Operator.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,12 @@ Operator(L::UniformScaling{Bool}, s::Space) = L.λ ? IdentityOperator(s) : ZeroO
628628
Operator(L::UniformScaling, d::Domain) = Operator(L, Space(d))
629629

630630
Operator{T}(f::Fun) where {T} =
631-
norm(f.coefficients)==0 ? zero(Operator{T}) : convert(Operator{T}, Multiplication(f))
631+
norm(f.coefficients)==0 ? zero(Operator{T}) : strictconvert(Operator{T}, Multiplication(f))
632632

633633
Operator(f::Fun) = norm(f.coefficients)==0 ? ZeroOperator() : Multiplication(f)
634634

635635
convert(::Type{O}, f::Fun) where O<:Operator = O(f)
636-
Operator{T}(A::Operator) where T = convert(Operator{T}, A)
636+
Operator{T}(A::Operator) where T = strictconvert(Operator{T}, A)
637637

638638

639639
## Promotion

0 commit comments

Comments
 (0)