I am glad to get two consistent answers within 1 minute of each other! On Wednesday, April 13, 2016 at 1:48:20 PM UTC-4, Matt Bauman wrote: > > If you're able to define a `size` method for the Associative types, then > I'd call them all AbstractArrays. > Yeah they are 1 dimensional and have size(A) = n. I am thinking that it is 0 on every index that hasn't been set yet, and the only valid indices are i in 1:n. I want to use Dict{Int,T} as a "lazily allocated" Vector{T}.
> I'd use a wrapper type like the last example in > http://docs.julialang.org/en/release-0.4/manual/interfaces/#abstract-arrays. > You could parameterize the `data` field so any associative type could be > wrapped. As a bonus: now it works like an array in Base code, too, without > changing any signatures. > So the SparseArray in the example would become immutable SparseArray{C<:Associative{N, T},T,N} <: AbstractArray{T,N} data::C dims::N end And then all my functions that currently use Array{T,N} can use AbstractArray{T,N} and then can use this SparseArray automagically? This gives an error ERROR: UndefVarError: N not defined immutable SparseArray2{C<:Associative,T,N} <: AbstractArray{T,N} data::C dims::NTuple{N,Int} end Works but then when constructing it you need to make sure that C, T, and N are consistent in the constructor logic because there is nothing in the type definition that would forbid you from instantiating a SparseArray2{Dict{Int, Float64}, Float32, 3}(Dict{Int, Float64}(), (2,3,4)) The problem is that data is not an Associative{NTuple{N, Int}, T}. Is there any way to specify this in the Type definitions? Or do you make a constructor that validates C <: Associative{NTuple{N, Int},T} at construction time? What would be the appropriate error to throw in this case? Thanks, James