Hi,
I am working on a mini-library that makes tabulation of arbitrary data
easier. The design that works well at the moment is just wrapping a
dictionary, which maps a Tuple of given type (and thus length) to a
subtype of Real. The Tuple type gives the allowed key types.
It could be defined like this:
immutable DynamicNamedArray{Tv <: Real, Tk <: Tuple}
elements::Dict{Tk,Tv}
ordering
function DynamicNamedArray{Tk,Tv}(elements::Dict{Tk,Tv}, ordering::NTuple)
@assert nfields(Tk) == length(ordering)
new(elements, ordering)
end
end
where ordering is another tuple, of functions, which is used for display
and conversion to NamedArray.
But suppose I want to incorporate the length of the key tuple Tk into
the type. I could use
immutable DynamicNamedArray{Tv <: Real, Tk <: Tuple, N}
elements::Dict{Tk,Tv}
ordering::NTuple{N,Function}
function DynamicNamedArray{Tk,Tv,N}(elements::Dict{Tk,Tv},
ordering::NTuple{N})
@assert nfields(Tk) == N
new(elements, ordering)
end
end
I can then make it a subtype of AbstractSparseArray{Tv,N}. But the
constructor is still used to enforce nfields(Tk) == N. I am wondering if
there is a way to do something like (mock code follows)
immutable DynamicNamedArray{N, Tv <: Real, Tk <: NTuple{N}}
...
end
so that the order would be enforced in the type.
If I am trying to do something nonsensical and better solutions exist,
please tell me.
Best,
Tamas