This tiny package allows defining custom array types that use type-aliases of Int
as their indices. When handling a big data structure with lots of indices, this prevents you from accidentally using the wrong index. (Well, it does not prevent it, but it prevents the code from compiling). Since each index potentially has its own type, this also allows you to use method dispatch to have the right function operate on the right index type. And strongly-typed indices also make the semantics of your code clearer.
The basic interface is quite simple:
using StrongArrays
@StrongInt MyIndex # creates a type alias for `Int`
MyVector = StrongVector{MyIndex}
V = MyVector([8, 13, 21])
V[1] # throws an exception
V[MyIndex(1)] # returns 8
(there is a longer and more complete example in the project’s README, linked below).
The basic indexing, iteration and broadcast operations are also supported; e.g. you can extract a slice, iterate over pairs()
, or add two arrays. (On the other hand, matrix multiplication – for compatible index types – is not implemented yet).
Since everything is written using trivial wrappers over the basic AbstractArray
functions, there should be no performance hit — this package is supposed to be of concern only to the compiler!
The package is here, it was just submitted for registration.
(Although I’m not quite completely satisfied with the name; I chose this one because ArraysWithStronglyTypedIndices
seemed a bit too cumbersome).