May 20, 2021
Here’s a nice thing I discovered today - kind of rediscovered, as I’d asked about this a while back but it’s only just clicked.
You can make a Pascal-style enumerated-type indexed array in Julia in 2 lines of code. All you have to do is overload the array indexing in the Base library:
If I have an enum
Base.getindex( X::IncomesArray, s::Incomes ) = getindex(X,Int(s)+1) # enums without explicit numbers start at 0... Base.setindex!( X::IncomesArray, x, s::Incomes) = setindex!(X,x,Int(s)+1)
Incomes to be used as an index:
inc[Wages] = 99 and so on. It’s not quite the same as Pascal since the array being indexed can be any array type and of any length, and you can always index by
Ints as well - there’s no obvious way of turning these things off.
The STBIncomes module now uses this trick.