I am trying to define a type (structure?) in Julia for a vector with three elements. I think the closest thing I found Optimally passing dimensions of fixed size array in julia and Declare the size of an array attribute in a type definition, however these are pre-0.6 since immutables are no longer a thing. Also, it just seems wrong.
The use case is that I know the size of the vectors my function will be taking and want to have
function myFunc(v::threeVec,u::threeVec)
Do stuff to u and v
end
Further searching led me constructors. https://docs.julialang.org/en/stable/manual/constructors/ In particular I saw the example
struct OrderedPair
x::Real
y::Real
OrderedPair(x,y) = x > y ? error("out of order") : new(x,y)
end
However this is a separate object, and even then, I'm not sure how I'd pass something like that into a function. I considered instead using triples since those have type Tuple(Int,Int,Int) however I'm going to be doing vector/matrix arithmetic on u and v so I'm rather not have to convert them.
I could have the vector length checked inside the function, but I read in the tips that its preferred that you use types for this due to the dispatcher. For this particular function, that is a reasonable way of doing it in this case, but in other use cases this might not be such a great idea so I'd like to do it the "right way" now.
ThreeVec
as in your example. You could implement a type yourself, but it seems unnecessary given the many array packages out there. – juliohm