JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.74k stars 5.49k forks source link

Index conversions are too darn complicated #23822

Closed mbauman closed 4 years ago

mbauman commented 7 years ago

I'm entirely at fault for this one, but as a public and documented interface we're fairly constrained in the non-breaking changes we can make. There are two functions and four (!!) levels of indirection here, making things particularly hard to reason about.

Note that Base.to_index is not exported but it's documented as a function you can specialize.

I think we should simplify this for 1.0. The reasons for this design are largely historical, but some of these are still true:

Not sure about the best way to resolve this (or if it's even worth futzing about), but I figured I'd open an issue to see what others think.

timholy commented 7 years ago

I think some of the complexity comes from us trying to be reasonably nice to the compiler: march through the indices once, making all the relevant substitutions. What that means is that we have to dispatch on many different possibilities. An alternative (one that may not work) would be to conceive of it as a series of passes: flatten (to expand CartesianIndex), uncolon, etc. I think the main snafu with this plan is Array{CartesianIndex}, which really can't be flattened.

Another potential approach that might help a bit would be to develop a bit more formal structure for tuple operations. In https://github.com/JuliaLang/julia/pull/23867#issuecomment-331834296 it was suggested to expand the tuple interface along such directions. So _maybetail would become drop(t, 1). #23730, #23676, #23659, and #23658 are also aimed at developing more "standard" interfaces for tuples. We've historically "buried" these lispy-tuple methods for fear of extending things inappropriately (perhaps originally inspired by the fact that Base.tail is not exported), but perhaps it's time to change that?

JeffBezanson commented 7 years ago

This can maybe be included in 1.0, but I'd be really careful with priorities here, as time is starting to run a bit short.

timholy commented 7 years ago

If we want to try to solve this by developing a more standard tuple interface, I put up #23875 as a potential prerequisite.

StefanKarpinski commented 7 years ago

Triage essentially reiterates what @JeffBezanson said a week ago: if this gets done, excellent, if not, then we're already in better shape in terms of extensibility compared to every other language. I should also mention that since the top priority for 1.0 is user-level stability, there is some room for evolution of things like extensible indexing as long as it doesn't break too much and we're careful about making sure packages keep working.

mbauman commented 4 years ago

We've been living with this for quite some time now, and I think the API is just fine, especially since maybetail(t) is now just spelled t[2:end].