What is the current level of support for array indices other than Int
? other than Integer
? What are the plans going forward to support (or not) these?
You can use non-integer indices but if you don’t have AbstractUnitRange
axes then you can’t be a subtype of an AbstractArray
and expect things to work. You can just be an indexable object that behaves mostly array-like.
What are you trying to do with non-integer indices? What would they mean for an array?
(If you are trying to denote some kind of continuous interpolation probably it makes more sense to use f(x)
than f[x]
. But if you define your own type of f
object you are free to define f[x]
to do whatever you want by overloading Base.getindex
.)
Is this supposed to work?
struct Three <: AbstractVector{Int} end
Base.size(::Three) = (0x3,)
Base.axes(::Three) = (0x3:0x5,)
Base.getindex(::Three, ::UInt8) = 3
three = Three();
for x in three
println(x)
end
# 3
# 3
# 3
for i in eachindex(three)
println(three[i])
end
# 3
# 3
# 3
println(three)
# ERROR: CanonicalIndexError: getindex not defined for Three
I’m trying to test the limits of the language, be silly, and fool around
Practically, I’m wondering what I’m allowed to assume about AbstractArrays and AbstractVectors when writing generic code.
you need to handle ::Int
:
https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array