Skip to content

Commit 3374258

Browse files
authored
Fix convert methods to always return the exact type (#104)
* add type assertions to convert * constant tests
1 parent 1563ab4 commit 3374258

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/Fun.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,23 @@ coefficient(f::Fun,::Colon) = coefficient(f,1:dimension(space(f)))
8080

8181

8282
convert(::Type{Fun{S,T,VT}},f::Fun{S}) where {T,S,VT} =
83-
Fun(f.space,convert(VT,f.coefficients))
84-
convert(::Type{Fun{S,T,VT}},f::Fun) where {T,S,VT} =
85-
Fun(Fun(f.space,convert(VT,f.coefficients)),convert(S,space(f)))
83+
Fun{S,T,VT}(f.space, convert(VT,f.coefficients)::VT)
84+
function convert(::Type{Fun{S,T,VT}},f::Fun) where {T,S,VT}
85+
g = Fun(Fun(f.space, convert(VT,f.coefficients)::VT), convert(S,space(f))::S)
86+
Fun{S,T,VT}(g.space, g.coefficients)
87+
end
8688

87-
convert(::Type{Fun{S,T}},f::Fun{S}) where {T,S} =
88-
Fun(f.space,convert(AbstractVector{T},f.coefficients))
89+
function convert(::Type{Fun{S,T}},f::Fun{S}) where {T,S}
90+
coeff = convert(AbstractVector{T},f.coefficients)::AbstractVector{T}
91+
Fun{S, T, typeof(coeff)}(f.space, coeff)
92+
end
8993

9094

9195
convert(::Type{VFun{S,T}},x::Number) where {T,S} =
92-
x==0 ? zeros(T,S(AnyDomain())) : x*ones(T,S(AnyDomain()))
96+
(x==0 ? zeros(T,S(AnyDomain())) : x*ones(T,S(AnyDomain())))::VFun{S,T}
9397
convert(::Type{Fun{S}},x::Number) where {S} =
94-
x==0 ? zeros(S(AnyDomain())) : x*ones(S(AnyDomain()))
95-
convert(::Type{IF},x::Number) where {IF<:Fun} = convert(IF,Fun(x))
98+
(x==0 ? zeros(S(AnyDomain())) : x*ones(S(AnyDomain())))::Fun{S}
99+
convert(::Type{IF},x::Number) where {IF<:Fun} = convert(IF,Fun(x))::IF
96100

97101
Fun{S,T,VT}(f::Fun) where {S,T,VT} = convert(Fun{S,T,VT}, f)
98102
Fun{S,T}(f::Fun) where {S,T} = convert(Fun{S,T}, f)

test/SpacesTest.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ import ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, V
1313

1414
f = Fun(space(f),[1.,2.,3.])
1515

16+
@testset "conversions" begin
17+
@testset for S in Any[typeof(space(f)), Any]
18+
T = Fun{S, Any, Any}
19+
fany = convert(T, f)
20+
@test fany isa T
21+
@test (@inferred oftype(f, fany)) isa typeof(f)
22+
end
23+
S = typeof(space(f))
24+
T = Fun{S, Any}
25+
fany = convert(T, f)
26+
@test fany isa T
27+
@test (@inferred oftype(f, fany)) isa typeof(f)
28+
end
29+
1630
@testset "real/complex coefficients" begin
1731
c = [1:4;]
1832
for c2 in Any[c, c*im]
@@ -89,4 +103,10 @@ import ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, V
89103
@test @inferred(domain(a^3)) == domain(a)^3
90104
@test_broken @inferred(points(a^3)) == vec(Vec.(points(a), points(a)', reshape(points(a), 1,1,4)))
91105
end
106+
107+
@testset "ConstantSpace" begin
108+
@test (@inferred convert(Fun, 2)) == Fun(2)
109+
f = Fun(2)
110+
@test (@inferred convert(Fun{typeof(space(f))}, 2)) == f
111+
end
92112
end

0 commit comments

Comments
 (0)