Type parameters which depend on other type parameters

Is there a way to specify a type parameter in a way that depends on another type parameter?

For example, a StaticMatrix takes in types {M,N,T,L}, where M,N are dimensions and L is the length of the array. Is there a way to set L=M*N? For example, if I try to define

struct foo{N <: Integer}
    A::SMatrix{N,N,Float64,N*N}
end

I get the error

ERROR: MethodError: no method matching *(::TypeVar, ::TypeVar)

Sorry, my last comment did not attack exactly your case, but I am also having the problem, it really seems that inside a struct definition the N is a typevar, has no * defined for it, even if after a instance of a concrete type like Foo{10} is able to get the value and make use of it.

GitHub - vtjnash/ComputedFieldTypes.jl: Build types in Julia where some fields have computed types makes this a bit easier to deal with.

2 Likes

If all you want is to guarantee its a square matrix, you don’t need to compute that last entry yourself, just do,

struct foo{N, SM<:SMatrix{N,N,Float64}}
    A :: SM
end

(note also its just N instead of N <: Integer).

3 Likes

@marius311 - thank you! This solves my specific problem.

@rdeits - thanks, I didn’t know about this package.

1 Like