Using `Enum`s as indices

sgaure
Sun, 16 Jun 2024 16:12:29 +0000
General Usage

I’m using enum’s for some purposes. In particular for indexing arrays. It works fine, except for some types of broadcasting:

a = [zeros(10) for _ in 1:4];
b = [zeros(10) for _ in 1:4];
@enum Count one=1 two three four

Base.to_index(c::Count) = Int(c)

a[1] .= b[1]
a[1] .= b[one]
a[one] .= b[one]

The last broadcast assignment fails with

ERROR: DimensionMismatch: cannot broadcast array to have fewer non-singleton dimensions

I can work around with

aone = a[one]
aone .= b[one]

What magic does it take to make this work without the workaround?

4 posts - 3 participants