Skip to content

allow getindex(::MyType, ::Colon) to be customized #9419

@rfourquet

Description

@rfourquet

And similarly for setindex!.
For example:

type A end
Base.getindex(a::A, ::Range) = 0
Base.getindex(a::A, ::Colon) = 1

julia> A()[:]
ERROR: `endof` has no method matching endof(::A)

This is worse if endof(::A) is defined, as then A()[:] chooses silently the unintended getindex method.

If allowing customization of Base.getindex(a::A, ::Colon) does not happen, I would request that a[:] is translated to a[beginof(a):endof(a)], as some indexable collections have an indice starting at something else than 1.

At the very least, the doc for getindex/setindex! should mention this special case, where "The syntax a[i,j,...] is converted by the compiler to getindex(a, i, j, ...)" does not seem true.

Also, in "base/subarray.jl", this method is defined: getindex{T}(v::AbstractArray{T,1}, ::Colon) = v, should it be removed or is it used?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions