Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/FixedSizeArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,29 @@ macro fixed_vector(name, parent)
size_or(::Type{$(name){S, T} where T}, or) where {S} = Size{(S,)}()
size_or(::Type{$(name){S, T}}, or) where {S, T} = (S,)
# Array constructor
@inline function (::Type{$(name){S}})(x::AbstractVector{T}) where {S, T}
@inline function $(name){S}(x::AbstractVector{T}) where {S, T}
@assert S <= length(x)
$(name){S, T}(ntuple(i-> x[i], Val(S)))
end
@inline function (::Type{$(name){S, T1}})(x::AbstractVector{T2}) where {S, T1, T2}
@inline function $(name){S, T1}(x::AbstractVector{T2}) where {S, T1, T2}
@assert S <= length(x)
$(name){S, T1}(ntuple(i-> T1(x[i]), Val(S)))
end

@inline function (::Type{$(name){S, T}})(x) where {S, T}
@inline function $(name){S, T}(x) where {S, T}
$(name){S, T}(ntuple(i-> T(x), Val(S)))
end


@inline function (::Type{$(name){S}})(x::T) where {S, T}
@inline function $(name){S}(x::T) where {S, T}
$(name){S, T}(ntuple(i-> x, Val(S)))
end
@inline function (::Type{$(name){1, T}})(x::T) where T
@inline function $(name){1, T}(x::T) where T
$(name){1, T}((x,))
end
@inline (::Type{$(name)})(x::NTuple{S}) where {S} = $(name){S}(x)
@inline (::Type{$(name)})(x::T) where {S, T <: Tuple{Vararg{Any, S}}} = $(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
@inline function (::Type{$(name){S}})(x::T) where {S, T <: Tuple}
@inline $(name)(x::NTuple{S}) where {S} = $(name){S}(x)
@inline $(name)(x::T) where {S, T <: Tuple{Vararg{Any, S}}} = $(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
@inline function $(name){S}(x::T) where {S, T <: Tuple}
$(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
end
$(name){S, T}(x::StaticVector) where {S, T} = $(name){S, T}(Tuple(x))
Expand Down
2 changes: 1 addition & 1 deletion src/SDiagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ diagtype(::Type{SDiagonal}) = SVector
# this is to deal with convert.jl
@inline (::Type{SD})(a::AbstractVector) where {SD <: SDiagonal} = SDiagonal(convert(diagtype(SD), a))
@inline (::Type{SD})(a::Tuple) where {SD <: SDiagonal} = SDiagonal(convert(diagtype(SD), a))
@inline (::Type{SDiagonal})(a::SVector{N,T}) where {N,T} = SDiagonal{N,T}(a)
@inline SDiagonal(a::SVector{N,T}) where {N,T} = SDiagonal{N,T}(a)

@generated function SDiagonal(a::StaticMatrix{N,N,T}) where {N,T}
expr = [:(a[$i,$i]) for i=1:N]
Expand Down
6 changes: 3 additions & 3 deletions src/SVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ compiler (the element type may optionally also be specified).
"""
const SVector{S, T} = SArray{Tuple{S}, T, 1, S}

@inline (::Type{SVector})(x::NTuple{S,Any}) where {S} = SVector{S}(x)
@inline (::Type{SVector{S}})(x::NTuple{S,T}) where {S, T} = SVector{S,T}(x)
@inline (::Type{SVector{S}})(x::T) where {S, T <: Tuple} = SVector{S,promote_tuple_eltype(T)}(x)
@inline SVector(x::NTuple{S,Any}) where {S} = SVector{S}(x)
@inline SVector{S}(x::NTuple{S,T}) where {S, T} = SVector{S,T}(x)
@inline SVector{S}(x::T) where {S, T <: Tuple} = SVector{S,promote_tuple_eltype(T)}(x)

# conversion from AbstractVector / AbstractArray (better inference than default)
#@inline convert{S,T}(::Type{SVector{S}}, a::AbstractArray{T}) = SVector{S,T}((a...))
Expand Down
30 changes: 15 additions & 15 deletions src/SizedArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ struct SizedArray{S <: Tuple, T, N, M} <: StaticArray{S, T, N}
end
end

@inline (::Type{SizedArray{S,T,N}})(a::Array{T,M}) where {S,T,N,M} = SizedArray{S,T,N,M}(a)
@inline (::Type{SizedArray{S,T}})(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
@inline (::Type{SizedArray{S}})(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
@inline SizedArray{S,T,N}(a::Array{T,M}) where {S,T,N,M} = SizedArray{S,T,N,M}(a)
@inline SizedArray{S,T}(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
@inline SizedArray{S}(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)

@inline (::Type{SizedArray{S,T,N}})() where {S,T,N} = SizedArray{S,T,N,N}()
@inline (::Type{SizedArray{S,T}})() where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}()
@inline SizedArray{S,T,N}() where {S,T,N} = SizedArray{S,T,N,N}()
@inline SizedArray{S,T}() where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}()

@generated function (::Type{SizedArray{S,T,N,M}})(x::NTuple{L,Any}) where {S,T,N,M,L}
if L != tuple_prod(S)
Expand All @@ -44,18 +44,18 @@ end
end
end

@inline (::Type{SizedArray{S,T,N}})(x::Tuple) where {S,T,N} = SizedArray{S,T,N,N}(x)
@inline (::Type{SizedArray{S,T}})(x::Tuple) where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)
@inline (::Type{SizedArray{S}})(x::NTuple{L,T}) where {S,T,L} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)
@inline SizedArray{S,T,N}(x::Tuple) where {S,T,N} = SizedArray{S,T,N,N}(x)
@inline SizedArray{S,T}(x::Tuple) where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)
@inline SizedArray{S}(x::NTuple{L,T}) where {S,T,L} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)

# Overide some problematic default behaviour
@inline convert(::Type{SA}, sa::SizedArray) where {SA<:SizedArray} = SA(sa.data)
@inline convert(::Type{SA}, sa::SA) where {SA<:SizedArray} = sa

# Back to Array (unfortunately need both convert and construct to overide other methods)
@inline (::Type{Array})(sa::SizedArray) = sa.data
@inline (::Type{Array{T}})(sa::SizedArray{S,T}) where {T,S} = sa.data
@inline (::Type{Array{T,N}})(sa::SizedArray{S,T,N}) where {T,S,N} = sa.data
@inline Array(sa::SizedArray) = sa.data
@inline Array{T}(sa::SizedArray{S,T}) where {T,S} = sa.data
@inline Array{T,N}(sa::SizedArray{S,T,N}) where {T,S,N} = sa.data

@inline convert(::Type{Array}, sa::SizedArray) = sa.data
@inline convert(::Type{Array{T}}, sa::SizedArray{S,T}) where {T,S} = sa.data
Expand All @@ -65,12 +65,12 @@ end
@propagate_inbounds setindex!(a::SizedArray, v, i::Int) = setindex!(a.data, v, i)

SizedVector{S,T,M} = SizedArray{Tuple{S},T,1,M}
@inline (::Type{SizedVector{S}})(a::Array{T,M}) where {S,T,M} = SizedArray{Tuple{S},T,1,M}(a)
@inline (::Type{SizedVector{S}})(x::NTuple{L,T}) where {S,T,L} = SizedArray{Tuple{S},T,1,1}(x)
@inline SizedVector{S}(a::Array{T,M}) where {S,T,M} = SizedArray{Tuple{S},T,1,M}(a)
@inline SizedVector{S}(x::NTuple{L,T}) where {S,T,L} = SizedArray{Tuple{S},T,1,1}(x)

SizedMatrix{S1,S2,T,M} = SizedArray{Tuple{S1,S2},T,2,M}
@inline (::Type{SizedMatrix{S1,S2}})(a::Array{T,M}) where {S1,S2,T,M} = SizedArray{Tuple{S1,S2},T,2,M}(a)
@inline (::Type{SizedMatrix{S1,S2}})(x::NTuple{L,T}) where {S1,S2,T,L} = SizedArray{Tuple{S1,S2},T,2,2}(x)
@inline SizedMatrix{S1,S2}(a::Array{T,M}) where {S1,S2,T,M} = SizedArray{Tuple{S1,S2},T,2,M}(a)
@inline SizedMatrix{S1,S2}(x::NTuple{L,T}) where {S1,S2,T,L} = SizedArray{Tuple{S1,S2},T,2,2}(x)

if isdefined(Base, :dataids) # v0.7-
Base.dataids(sa::SizedArray) = Base.dataids(sa.data)
Expand Down
7 changes: 3 additions & 4 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ end

Base.show(io::IO, ::Size{S}) where {S} = print(io, "Size", S)

#= There seems to be a subtyping/specialization bug...
Size(a::T) where {T<:AbstractArray} = Size(T)
function Size(::Type{SA}) where {SA <: StaticArray} # A nice, default error message for when S not defined
error("""
The size of type `$SA` is not known.
Expand All @@ -82,9 +82,8 @@ function Size(::Type{SA}) where {SA <: StaticArray} # A nice, default error mess
SMatrix(m) # this error
SMatrix{3,3}(m) # correct - size is inferrable
""")
end =#
Size(a::T) where {T<:AbstractArray} = Size(T)
Size(::Type{<:StaticArray{S}}) where {S} = Size(S)
end
Size(::Type{SA}) where {SA <: StaticArray{S}} where {S<:Tuple} = Size(S) # S defined as a Tuple
Copy link
Member

@fredrikekre fredrikekre Jun 25, 2018

Choose a reason for hiding this comment

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

This gives UndefVarError: S not defined:

julia> convert(SVector, [1,2,3])
ERROR: UndefVarError: S not defined
Stacktrace:
 [1] Size(::Type{SArray{Tuple{S},T,1,S} where T where S}) at /Users/Fredrik/.julia/packages/StaticArrays/Jd49/src/traits.jl:86
 [2] length(::Type{SArray{Tuple{S},T,1,S} where T where S}) at /Users/Fredrik/.julia/packages/StaticArrays/Jd49/src/abstractarray.jl:2
 [3] convert(::Type{SArray{Tuple{S},T,1,S} where T where S}, ::Array{Int64,1}) at /Users/Fredrik/.julia/packages/StaticArrays/Jd49/src/convert.jl:22
 [4] top-level scope at none:0

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting. We tested this with 0.6, where it works as intended.

I wonder if this is actually a dispatch bug in 0.7. That line explicitly requires that S<:Tuple, and when S is undefined I don't see how that statement could be true.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm, it should work...

julia> abstract type AType{T} end

julia> foo(::Type{A}) where A<:AType = "parameter missing"
foo (generic function with 1 method)

julia> foo(::Type{A}) where A<:AType{T} where T<:Tuple = "parameter present"
foo (generic function with 2 methods)

julia> struct C{T} <: AType{T} end

julia> foo(C)
"parameter missing"

julia> foo(C{Tuple{1,2}})
"parameter present"

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't what the exact problem is here, but note that, continuing with your definitions:

julia> foo(C{Union{T,Tuple}} where T <: Tuple)
"parameter present"

julia> foo(::Type{A}) where A<:AType{T} where T<:Tuple = "parameter present: $T"
foo (generic function with 2 methods)

julia> foo(C{Union{T,Tuple}} where T <: Tuple)
ERROR: UndefVarError: T not defined

So it's always safer to add an @isdefined T conditional.

Copy link
Member

Choose a reason for hiding this comment

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

Opened #452 to track this.

@pure Size(::Type{<:AbstractArray{<:Any, N}}) where {N} = Size(ntuple(_ -> Dynamic(), N))

struct Length{L}
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@
@test @inferred(convert(Array, ma)) == a
@test @inferred(convert(Array{Int}, ma)) == a
@test @inferred(convert(Array{Int,2}, ma)) == a

try
convert(SVector, [1,2,3])
catch err
@test isa(err, ErrorException)
@test startswith(err.msg, "The size of type `StaticArrays.SArray{Tuple{S},T,1,S} where T where S` is not known.")
end
end
@test_throws Exception Length{2.5}()
@test Length(2) == Length{2}()
Expand Down
2 changes: 2 additions & 0 deletions test/fixed_size_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ const binaryOps = (
@test r[j] == op(v1[j], v2[j])
end
end
catch
end
end
end
Expand All @@ -436,6 +437,7 @@ const binaryOps = (
@test v[i] == op(t[i])
end
end
catch
end
end
end
Expand Down