-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Update methods.rst #10769
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
Update methods.rst #10769
Conversation
The change mentions a possible side-effect of the implementation of optional arguments that was discussed on julia-users: https://groups.google.com/forum/#!topic/julia-users/gNMILY7s95c
It's good to mention this in the manual, but I think the underlying issue should be fixed. @JeffBezanson, @StefanKarpinski? |
Pending contradiction from Jeff or Stefan I don't think there is a problem. Julia is a multiple dispatch language, the type of the parameters decides the method to use. Optional parameters (note Julia does not call them "default values") are implemented as multiple methods, ie
is
Clearly the type of For programmers from other languages thinking in non-Julian terms, it perhaps could be expressed as "the default values are calculated each call (like C++ not like Python) and so they impact method dispatch". It is documented that this is how optional parameters work, but maybe it needs to be better highlighted in the "Optional Arguments" section of functions, rather than being relegated to an easily overlooked (see Methods) at the very end. |
Right, "default values" is not the proper terminology here, thanks for pointing that out. I'll change that. In any case I'm not sure whether I've done the pull request correctly, as one of the builds seems to have failed. If documentation is the answer, I do believe the section on Methods is more appropriate, if only because the notions of methods and dispatch are not discussed yet in the section on Functions. It is difficult to briefly explain the issue without those concepts beyond what is already written. Strictly speaking, the behaviour is actually clear from what is said in |
It failed the whitespace check. Copying from the build log:
You may also want to amend your commit message to include the string |
Yes, thats what I was suggesting should also be more prominent. |
Could someone tell me why this behaves the way it does?
I've read through the documentation proposed here and still don't get it. The docs say that the default values merely create additional methods with variable numbers of arguments, and I see them with |
See my comment above, |
Sorry for the lack of response here on my behalf - I have only intermittent and poor internet access this week (holiday), but I still intend to take the comments above into account, fix the pull request and update the documentation. |
I did the original change on the website only, which in hindsight was not the best idea as apparently that makes it more difficult to amend the commit message (to include [av skip]). |
Although travis is currently usefully telling you that you have a whitespace error :) |
Yes, good point :-) |
@elextr thanks very much for the explanation. I understand it, though I'm not quite sure I agree with the behavior. It seems to me that this way of handling optional parameters can have some very ugly side effects (though I confess to being too tired to provide a documented example right now, I'm thinking of a case where you're relying on some external [non-julian] process to provide data to a function, and that process is itself not type-stable). |
As I said above, I think this should be changed. The current behavior is a leaky abstraction: it looks like all you are doing is defining som default arguments for your method, but in practice you have to be aware of and consider how multiple dispatch works and which other methods have been defined for the same function. If we leave this kind of trap in, I'm not quite sure we should provide the mechanism of default arguments at all. |
at a minimum, I think this should throw a warning:
|
@toivoh, do you have a proposal for how you'd want to see this fixed? |
I guess one option would be that the additional methods implied by default arguments use invoke to call the specific method that the defaults are provided for. I have a nagging suspicion that this might cause other equally bad or worse problems though... |
Well, my thought was that the solution would have to stay partially outside the dispatch machinery. We would still create a method for each possible number of arguments, but then we would also create a hidden function containing the actual method body, which these would call directly. |
@toivoh the only way I can see is to compute the default values at compile time so Its also going to be hard to make that change without breaking any code that inadvertently relies on the current behaviour since it would be hard to deprecate. |
I don't see why you would have to evaluate the defaults at definition time; my suggestion only changes what the created methods call after they have evaluated their default arguments. Deprecation is more of an issue, but given that this is an accidental feature that has not been advertised and has just been discovered, I'm hoping that it's not relied on a lot and that a public announcement would be enough. |
@toivoh can you explain your suggestion more completely, I don't understand what you actually propose. As I pointed out above it is documented, just split across two places (for good reason) so its not crystal clear. |
I guess this issue is still a bit controversial. In the meantime I updated the pull request with some of the comments made above. I kept the changes to the manual rather minimal, because a thorough discussion of this design and its implications, with some of the examples given above, would make the manual rather complicated. Is it reasonable? |
Sorry for hijacking the thread. Didn't intend any criticism on the doc
update.
|
Hey, no problem, discussions tend to be distributed here I gather. It is very handy to have these links to other issues for a complete picture (thanks @mbauman). |
Test failures are not whitespace, so that failure should not hold this up. |
Cool, thanks. |
The change mentions a possible side-effect of the implementation of optional arguments that was discussed on julia-users:
https://groups.google.com/forum/#!topic/julia-users/gNMILY7s95c