Skip to content

Commit 324308b

Browse files
author
Andy Ferris
committed
Progress towards new subtype compatibility for v0.6
This addresses some of the worst problems from Jeff's new subtype algorithm. Currently a lot of the constructor code is incorrect in this branch. I'm hoping the new, more consistent subtyping algorithm will help to acheive what we want, but the horrible hacks for v0.5 are not forward-compatible.
1 parent 2fe3ebf commit 324308b

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

src/FieldVector.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ abstract FieldVector{T} <: StaticVector{T}
2121
# Is this a good idea?? Should people just define constructors that accept tuples?
2222
@inline (::Type{FV}){FV<:FieldVector}(x::Tuple) = FV(x...)
2323

24-
@pure size{FV<:FieldVector}(::Union{FV,Type{FV}}) = (length(FV.types),)
25-
@pure length{FV<:FieldVector}(::Union{FV,Type{FV}}) = size(FV)[1]
24+
@pure size{FV<:FieldVector}(::FV) = (length(FV.types),)
25+
@pure size{FV<:FieldVector}(::Type{FV}) = (length(FV.types),)
26+
@pure length{FV<:FieldVector}(::FV) = size(FV)[1]
27+
@pure length{FV<:FieldVector}(::Type{FV}) = size(FV)[1]
2628

2729
@inline getindex(v::FieldVector, i::Integer) = getfield(v, i)
2830
@inline setindex!(v::FieldVector, x, i::Integer) = setfield!(v, i, x)

src/FixedSizeArrays.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ immutable Point{S, T} <: StaticVector{T}
5858
data::NTuple{S, T}
5959
end
6060

61-
@inline (::Type{Point}){S}(x::NTuple{S}) = Point{S}(x)
61+
if VERSION < v"0.5+"
62+
@inline (::Type{Point}){S}(x::NTuple{S}) = Point{S}(x)
63+
end
6264
@inline (::Type{Point{S}}){S, T}(x::NTuple{S,T}) = Point{S,T}(x)
6365
@inline (::Type{Point{S}}){S, T <: Tuple}(x::T) = Point{S,promote_tuple_eltype(T)}(x)
6466

src/abstractarray.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
typealias StaticScalar{T} StaticArray{T,0}
22

3-
@pure length{T<:StaticArray}(a::Union{T,Type{T}}) = prod(size(a))
4-
@pure length{T<:StaticScalar}(a::Union{T,Type{T}}) = 1
3+
@pure length{T<:StaticArray}(a::T) = prod(size(a))
4+
@pure length{T<:StaticArray}(a::Type{T}) = prod(size(a))
5+
@pure length{T<:StaticScalar}(a::T) = 1
6+
@pure length{T<:StaticScalar}(a::Type{T}) = 1
57

6-
@pure function size{T<:StaticArray}(a::Union{T,Type{T}}, d::Integer)
8+
@pure size{T<:StaticArray}(::T, d::Integer) = size(T, d)
9+
@pure function size{T<:StaticArray}(a::Type{T}, d::Integer)
710
s = size(a)
8-
return (d <= length(s) ? s[d] : 1)
11+
return d <= length(s) ? s[d] : 1
912
end
1013

1114
# This seems to confuse Julia a bit in certain circumstances (specifically for trailing 1's)
@@ -14,7 +17,8 @@ end
1417
1 <= ii <= length(a) ? true : false
1518
end
1619

17-
Base.linearindexing{T<:StaticArray}(::Union{T,Type{T}}) = Base.LinearFast()
20+
Base.linearindexing(::StaticArray) = Base.LinearFast()
21+
Base.linearindexing{T<:StaticArray}(::Type{T}) = Base.LinearFast()
1822

1923
# Default type search for similar_type
2024
"""

src/arraymath.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ Base.promote_op{Op,T<:Number,A<:StaticArray}(op::Op, ::Type{T}, ::Type{A}) = sim
6969
@inline (|){T}(a1::Number, a2::StaticArray{T}) = broadcast(|, a1, a2)
7070
@inline ($){T}(a1::Number, a2::StaticArray{T}) = broadcast($, a1, a2)
7171

72-
73-
@generated function Base.zeros{SA <: StaticArray}(::Union{SA,Type{SA}})
72+
@inline zeros{SA <: StaticArray}(::SA) = zeros(SA)
73+
@generated function Base.zeros{SA <: StaticArray}(::Type{SA})
7474
s = size(SA)
7575
T = eltype(SA)
7676
if T == Any
@@ -82,9 +82,9 @@ Base.promote_op{Op,T<:Number,A<:StaticArray}(op::Op, ::Type{T}, ::Type{A}) = sim
8282
$(Expr(:call, SA, Expr(:tuple, v...)))
8383
end
8484
end
85-
@inline Base.zero{SA <: StaticArray}(a::Union{SA,Type{SA}}) = zeros(a)
8685

87-
@generated function Base.ones{SA <: StaticArray}(::Union{SA,Type{SA}})
86+
@inline Base.ones{SA <: StaticArray}(::SA) = ones(SA)
87+
@generated function Base.ones{SA <: StaticArray}(::Type{SA})
8888
s = size(SA)
8989
T = eltype(SA)
9090
if T == Any
@@ -97,7 +97,8 @@ end
9797
end
9898
end
9999

100-
@generated function Base.fill{SA <: StaticArray}(val, ::Union{SA,Type{SA}})
100+
@inline Base.fill{SA <: StaticArray}(val, ::SA) = fill(val, SA)
101+
@generated function Base.fill{SA <: StaticArray}(val, ::Type{SA})
101102
l = length(SA)
102103
T = eltype(SA)
103104
expr = [:valT for i = 1:l]

src/linalg.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ end
216216
@inline hcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}...) =
217217
hcat(hcat(a,b), c...)
218218

219+
@inline Base.zero{SA <: StaticArray}(a::SA) = zeros(SA)
220+
@inline Base.zero{SA <: StaticArray}(a::Type{SA}) = zeros(SA)
221+
219222
@generated function one{SM <: StaticArray}(::Type{SM})
220223
s = size(SM)
221224
if (length(s) != 2) || (s[1] != s[2])

src/util.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ end
4343
@inbounds return ntuple(i -> convert(T1,a[i]), Val{N})
4444
end
4545

46-
# TODO try and make this generate fast code
47-
@inline convert(::Type{Tuple}, g::Base.Generator) = (g...)
48-
@inline function convert{N}(::Type{NTuple{N}}, g::Base.Generator)
49-
@boundscheck if length(g.iter) != N
50-
error("Array of length $(length(a)) cannot be converted to a $N-tuple")
51-
end
46+
if VERSION < v"0.5+"
47+
# TODO try and make this generate fast code
48+
@inline convert(::Type{Tuple}, g::Base.Generator) = (g...)
49+
@inline function convert{N}(::Type{NTuple{N}}, g::Base.Generator)
50+
@boundscheck if length(g.iter) != N
51+
error("Array of length $(length(a)) cannot be converted to a $N-tuple")
52+
end
5253

53-
@inbounds return ntuple(i -> g.f(g.iter[i]), Val{N})
54+
@inbounds return ntuple(i -> g.f(g.iter[i]), Val{N})
55+
end
5456
end
5557

5658
#=

0 commit comments

Comments
 (0)