I’ve recently been learning source codes of pkg FixedEffectModels
.
if false
Xall = Combination(y, Xexo, Xendo, Z)
else
Xall = Combination(y, Xexo)
println(typeof(Xall)) # it can been print
println("Xall") # it can been print
println(Xall) # it cause error
end
it reports a error as follows:
CanonicalIndexError: getindex not defined for Combination{Float64}
these code in a cell with notebook of VS code can replicate this error:
struct Combination{T} <: AbstractMatrix{T}
A::Tuple
cumlength::Vector{Int}
end
function Combination(A::Union{AbstractVector{T}, AbstractMatrix{T}}...) where {T}
Combination{T}(A, cumsum([size(x, 2) for x in A]))
end
Base.size(c::Combination) = (size(c.A[1], 1), c.cumlength[end])
Base.size(c::Combination, i::Integer) = size(c)[i]
function Base.view(c::Combination, ::Colon, j)
index = searchsortedfirst(c.cumlength, j)
newj = index == 1 ? j : j - c.cumlength[index-1]
view(c.A[index], :, newj)
end
Combination([1,2,3], [4,5,6])
and in a new cell with followed code works well.
# new cell
Combination([1,2,3], [4,5,6]);
So
- why this error occurs?
- Is it cause by the lack of proper show function for struct
Combination
- if it’s true, is it a flaw of julia?
- a more complicated question is that how can I know how many methods current object/instance can use?