I ended up banging my head against this yesterday, I guess I’m interested by both the how to do this properly as well as the why did the other ways not work in order to get better at this parametric type business.
Aim: have an object that’s a subtype of AbstractVector
which keeps track of two things:
- a support (vector of, say, symbols) of length
c
- an array of weights of size
n x c
(e.g. scores returned by a classifier)
so each “element” of the vector corresponds to a pair (support, weights) where support and weights have the same length c
.
What I tried to express in a parametric way is that the support and the number of columns of the arrays are tied. So I tried:
julia> struct Foo{W<:AbstractArray{<:Real, C}, S<:NTuple{C,Symbol}} where {C} <: AbstractVector
weights::W
support::S
end
ERROR: syntax: invalid type signature
Stacktrace:
[1] top-level scope at none:1
crap. (By the way that error message is a bit unhelpful.).
So I tried something simpler to try to build from there:
julia> struct Foo{T, C} <: AbstractVector
weights::Array{T,C}
support::NTuple{C,Symbol}
end
ERROR: invalid subtyping in definition of Foo
Stacktrace:
[1] top-level scope at none:1
after trying many more sillier variants and eventually coming to the conclusion that clearly I didn’t understand what I was doing, I decided to ask for help, so here I am, help!
PS: in the mean time I’m using something less constraining with an internal constructor that checks that dimensions match but it feels unsatisfactory.