Solvedjulia Rename getindex and setindex! to operator form

A discussion on slack suggested that getindex is confusing and should perhaps be called getatindex (which is not quite as readable). But if we could write this in operator form

julia> Base.:(==)
== (generic function with 165 methods)

julia> Base.:([])
ERROR: syntax: invalid syntax "Base.[]" around REPL[2]:1
 [1] top-level scope
   @ REPL[2]:1

(it's good that it throws an error now, that means it's available) then we could add a custom array type and define

Base.:([])(A::MySpecialArray, i::Int) = ...

It would seem this doesn't even have to wait for Julia 2.0; that could be when we delete the older names.

26 Answers

✔️Accepted Answer

I think getindex(v, i) is maybe counterintuitive if you think too hard about it for like one or two encounters and then would be fine. On the other hand, code like

Base.:([])(v::MyType, i...) = #...

is at risk of being confusing and opaque every time you look at it. This idea is cute, but imo deprecating getindex in favour of it is a cure far worse than the disease.

Other Answers:

In thinking about the potential implications, one that arises is that [] alone is currently interpreted as Base.vect()

This is a key point to me. The syntax [] already means something else, so reusing it for something slightly different is hardly a model of clarity. The syntax a[b] is also not really "operator syntax", so calling it [] to me has the dual problems that (1) it's already a strange non-standard way to name something, and (2) it doesn't correspond directly to how it's used. If the indexing syntax were a [] b it would be ok I guess.

getatindex vs. getindex seems a bit pedantic to me. Sure, you can argue that it's more accurate, but I bet nobody would guess it either. But, there are some precedents for getindex getting an index instead of an element, e.g. in java. Python uses __getitem__. We have getkey, so maybe getvalue, to pair with that? I'm willing to consider a renaming like that for 2.0.

I really don't think throwing around var"get[]" is going to help with anything.

But like... Why?

That seems like a pretty good name for it.

More Issues: