Is there is an easy way to do something like this
struct TensorField{T,ND,NC} <: AbstractArray{T,ND+NC}
end
I need to define a scaler/vector/tensor field in 1D/2D/3D and I don’t want to define a type for each dimension.
Is there is an easy way to do something like this
struct TensorField{T,ND,NC} <: AbstractArray{T,ND+NC}
end
I need to define a scaler/vector/tensor field in 1D/2D/3D and I don’t want to define a type for each dimension.
My guess is you are trying to indicate the number of covariant and contravariant indices/dimensions with ND
and NC
, and they must add up to the total number of array dimensions N
. You can’t add numeric type variables in type definitions, but you can add their concrete values inside type constructors. Thus, something like the following should work.
struct TensorField{T,ND,NC,N} <: AbstractArray{T,N}
...
function TensorField{T,ND,NC} where {T,ND,NC}
...
new{T, ND, NC, ND+NC}(...)
end
end
This should essentially enforce the invariant that ND+NC == N
for any instance of TensorField
. You could alternatively use an outer constructor and include a check in the inner constructor that ND+NC == N
or else raise an exception.
You can’t do this yet, but there have been discussions of supporting it someday: Proposal: Defer calculation of field types until type parameters are known · Issue #18466 · JuliaLang/julia · GitHub