-
-
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
Dispatch method selection (RangeIndex... more specific than Integer?) #795
Comments
For reference, here's the function sort orders, most to least specific as reported by Julia. Note there are only three dispatches listed after defining the first four functions, even though the
And with the additional dispatch added in the transcript, still four, not five:
|
Doesn't |
Oh, it's overriding the first method. That's why it's accessible. I didn't realize none of the tests came up with the "f1" response. So it's just a question of why the type |
RangeIndex seems to be |
Then Union(Int,Integer) should not resolve to Integer. But I don't want to lose sight of the other aspect of this: if I provide a general method:
but then also a specialization for 1 and 2 arguments,
then to me it seems that the number of arguments should trump other considerations, as long as there is a sensible resolution for the one- and two-argument input. |
|
I've provided a number of conversions to abstract types. I don't see that there's a problem with that. To me, it means, "convert this value to some kind of Integer, but I don't care what kind". |
I didn't say there's a problem with it, just that that particular use does
|
OK, so what to do about this? I should say this is not purely academic: it seems to be important for my ongoing work on issue 765, where there are specializations of ref and assign for up to 4 dimensions. Now that I'm writing optimized versions that operate on RangeIndexes, there are turning out to be all sorts of nasty surprises in terms of which variants of the functions get called. Option #1: no change in "core julia". For the 2-input syntax I provide 4 versions: Option #2: we redefine RangeIndex this way: Option #3: For a function which can take arbitrary # of inputs, in dispatch give higher priority to the # of arguments than to the most concrete type. Not sure whether this would have unintended side effects. Option #4 (science fiction?): provide a mechanism for providing manual "hints" about the resolving dispatch ambiguity, that allow one to achieve #3 in particular circumstances without affecting policy more globally. |
Out of curiosity, why can't Union(Int, Integer) be different from just Integer? I presume Union does not work by "promoting" to the common parent; you can have a Union(Int8, Float64) without needing to bring in all bitstypes. If Int works differently from Integer with regards to method dispatch, it seems inconsistent not to be able to specify that it's a legitimate specialization for a particular argument. |
|
Tim, I'm not sure which of your four options is best, but 2 seems like just a work-around (although perhaps a pragmatic one), while 1 is pretty unsatisfying; 4 doesn't feel very Julian to me, which leaves 3. There were reasons to set the priorities the way they are now, but I can't recall what they were. When you get into varargs cases and dipatch, things get pretty hard to reason about. |
Thanks for the explanation, that makes sense. I'm a neophyte when it comes to thinking about type specifications in programming languages. But I have to ask, does Union have a practical use aside from controlling dispatch? If not, shouldn't its behavior mimic the choices made by the dispatch mechanism? If Union does need to work the way it's working, then is it worth considering having a DispatchUnion which does mimic dispatch's choices? I guess that's basically option 4 in disguise. |
Union is actually originally intended for everything except dispatch, but it happens to work there too. The concept you want here is "Integer, but treat as Int in ambiguous cases", not Union. I think the best option is one not on the list: only use Option 3 is reasonable but there have already been cases where the exact opposite priority behavior was wanted. In a case like |
Sounds good to me. Given how central arrays are to everything in Julia, we'll quickly learn whether anything breaks. |
Not sure if this is a bug or a design issue, but I would argue that in method dispatch insufficient priority is given to the # of arguments. I found the following somewhat surprising:
So even the explicit
f(i::Union(Int,Integer))
is not enough to "override" the genericf(ind::RangeIndex...)
variant; it requires defining anInt
-specific variant. TheInt
-specific variant can be defined before or after the others, it does not matter which.The text was updated successfully, but these errors were encountered: