struct Delay{T}
head_idx::Int
arr::Vector{T}
function Delay{T}(length::Int) where T
if ispow2(length)
arr = zeros(T,length)
new(0, arr)
else
throw(ArgumentError("Length is not power of 2"))
end
end
end
and I implemented the getindex function
function getindex(A::Delay{T},idx) where T
if idx > 0
throw(ArgumentError("Only accepts negative indexes"))
else
return 1
end
end
then I define test as
test = Delay{Float32}(8)
When I try to use test[1] it errors with ERROR: MethodError: no method matching getindex(::Delay{Float32}, ::Int64) but if I use getindex function directly it works
You need to explicitly import the Base.getindex function:
julia> function Base.getindex(A::Delay{T},idx) where T
if idx > 0
throw(ArgumentError("Only accepts negative indexes"))
else
return 1
end
end
julia> test[-1]
1
Generalizing, what functions should I implement for my array type? In Python, there is this: 3. Data model โ Python 3.10.6 documentation which states which methods that I should implement so that my class acts like a list, is there a documentation like this for Julia?