-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rename getindex
and setindex!
to operator form
#42525
Comments
But like... Why? |
The name In the near term, it may improve the legibility of code if this was more clearly connected with the square bracket syntax using an alias. Could we turn |
I think 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 |
In thinking about the potential implications, one that arises is that |
Yes, I fear In contrast, |
The
|
julia> macro operator_str(str)
esc(Symbol(:operator, str))
end
@operator_str (macro with 1 method)
julia> operator"[]"
ERROR: UndefVarError: operator[] not defined
Stacktrace:
[1] top-level scope
@ REPL[2]:1
julia> operator"[]" = getindex
getindex (generic function with 208 methods)
julia> operator"[]"(rand(3), 1)
0.10244386495710267 How do you distinguish between setindex! and getindex here though? In C++ that's through |
This is a key point to me. The syntax
|
Couldn’t we just do this lexically as well? Lower it to |
We used to do exactly that, but it changed in a grand reckoning on lowering pre-1.0 (breaking TwoBasedIndexing.jl). The informal rule is that we shouldn't lower to something you didn't write. The rationale here is well-motivated, IMO. You wrote So using a name that resembled |
The not-yet-implemented plan there was to lower to |
I dug and found the inception of the name Consider that we also have
|
TL;DR An alias for While the title of this issue suggests renaming, the present question is really if there should be an alias. We are all agreed that this is not the time for a renaming and deprecation to occur. It either should have been done very early on (0.2) or later in a 2.0. An alias that uses the characters For example, applied to Lines 56 to 57 in d633726
Another example brought up in Slack, is its use in Line 403 in d633726
In each of the above examples, perhaps it would have been intuitive to use a method name that refers to square brackets rather than indices. If examples such as this already occur in By adding an alias that has square brackets in the name, we could increase the human comprehension of code, especially when the method is being used in a manner that focuses on the syntax rather than the indexing operation. |
What alias would you propose for setindex! then for symmetry? |
Since we don't like calling this an operator, how about |
I really don't think throwing around |
Abandoning any attempt to relate the name to the syntax or "operator" as per the original post and focusing on just clarifying what the operation does, how about I chose "element" because we have precedent within Julia and ancestor languages. How many elements does an array have? What is the element type of array? julia> zeros(2)
2-element Vector{Float64}:
0.0
0.0
julia> eltype(zeros(2))
Float64 https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals
https://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/scheme_8.html#SEC72
https://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/scheme_9.html#SEC82
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
|
That seems like a pretty good name for it. |
You could argue that the value returned by That is, Seen that way, |
while |
I am not advocating for deprecation of |
this is even more confusing than deprecation because now there appear to be 4 things. I think core devs time is far better spent on other things. This discussion (the "what names is slightly more accurate on first read" part, not the |
Could we add some kind of facility to deal with brackets in general? |
How about |
Let's just close this bike shed.
|
A discussion on slack suggested that
getindex
is confusing and should perhaps be calledgetatindex
(which is not quite as readable). But if we could write this in operator form(it's good that it throws an error now, that means it's available) then we could add a custom array type and define
It would seem this doesn't even have to wait for Julia 2.0; that could be when we delete the older names.
The text was updated successfully, but these errors were encountered: