I m trying to figure out how to define the dimensions of a tuple when calling a function
or contructing a struct
when the arguments contain a NTuple
type.
If I leave the dimension variable (which defines the size of the Tuple
) "free" (<:Any), everything works and it is of an Integer
subtype. If I try to define it as any Integer
subtype I get an error that the dispatch cannot match a method.
Here s a concrete example:
Say that I want to generate several variables of a vector
or tuple
type and force the same dimensions nDims
. Here is a "stupified" version where all other things are simplified:
struct GridStruct{nDims}
NumCells :: NTuple{nDims, Integer}
DomainLength :: NTuple{nDims, AbstractFloat}
x :: Base.AbstractVecOrTuple{AbstractFloat}
y :: Base.AbstractVecOrTuple{AbstractFloat}
z :: Base.AbstractVecOrTuple{AbstractFloat}
function GridStruct(DomainLength::NTuple{nDims,AbstractFloat},NumCells::NTuple{nDims,Integer}) where nDims
x=GenerateCoordinate(NumCells[1], DomainLength[1]);
y=GenerateCoordinate(NumCells[2], DomainLength[2]);
z=GenerateCoordinate(NumCells[3], DomainLength[3]);
new{typeof(nDims)}(NumCells,DomainLength,xf,xc,yf,yc,zf,zc)
end
end
You can set GenerateCoordinate
to be whatever you want, like a simple uniform linear distribution:
GenerateCoordinate(N::Integer,L::AbstractFloat)=Base._linspace(0.0,L,N)
My question revolves around the definition of nDims
here: when I let it be "free" (so not defined", everything works fine and when I test the type of nDims
- it is indeed a subtype of Integer
(Int64
on my system) and has the expected value of 3 (as this is a 3D space).
If I try to define nDims
in any way, say nDims<:Integer
, I get an error that there is "no method" which matches:
ERROR: MethodError: no method matching Main.GridGenModule.GridStruct(::Tuple{Float64, Float64, Float64}, ::Tuple{Int64, Int64, Int64})
If I use nDims<:Any
, it obiously works - but there is nothing between ::Number
and ::Any
that I know of... And from what I can see, it is not any other type either.
I m trying to understand why defining nDims
(i.e. nDims<:Integer
) causes issues.
I ve seen this question that is related but there was no explanation or content regarding the issue I m asking about. I know how to get it to work, this is more for me to understand the "mechanics" of this.
Thank you so much!