-
Notifications
You must be signed in to change notification settings - Fork 5
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
Variadic Function Arguments #1
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"A variadic function parameter has at least one item" - I don't understand what this is trying to say. Are we talking about function declarations or function calls? Static or dynamic?
"that function defines a set of functions" - we seem to be using the word "function" here in two different senses. Needs greater precision.
If a function is declared with one argument, and that argument is variadic, and the type is a type that allows arrays (for example item()), and we pass an empty array [], I can't see how we distinguish whether we are passing [] as the value of the first argument, or whether we are passing no arguments.
The automatic coercion of sequence to array makes this especially problematic.
This was meaning that the definition of
Here,
I will update the proposal to use the latter behaviour (allowing no arguments to be passed to a variadic parameter).
The former is the function declaration, the latter is the resulting (function, arity) pairs that are effectively added to the statically known functions. I am saying effectively here as it is impractical to add an undetermined number of functions to the static context at the point the function is declared. XQuery processors already handle this with I will update the proposal to clarify the wording and behaviour.
That is a good point. The intention behind this was to make it easier to write recursive functions that call the function with 1 fewer argument each step in the recursion. I think it would make sense for this proposal to remove that logic and just stick to the mapping from the function declaration to the statically known functions. This would mean that given:
then I'll update this proposal accordingly. I will also consider writing a separate proposal to support the ability to "unpack" sequences or arrays to function arguments like can be done in other languages. Thanks for the review. |
…y/sequence expansion to function arguments.
…c function parameter.
I have updated the proposal to address your feedback, and to improve the wording. |
One way to allow sum([1,2,3]) as well as sum(1,2,3) would be to have a function expath:aggregator(f) (find a better name if you can) which takes the variadic function as input and produces the array-accepting function as its result. |
And here's another idea. Declare the function as |
I'm not immediately opposed to using an annotation to specify variadic arguments. There are some issues that would need to be resolved for me to support it fully:
One possibility would be something like:
Here, the last parameter would be variadic and add This would still mean that the solution would be XQuery-specific, and does not provide a mechanism to access the variadic arguments. Access to the arguments could be done via The parameters to array behaviour is how languages like Java and C# implement variadic arguments (and C++ for variadic template types). |
My 2 CentsHmmm... I want to think a bit about the initial impetus for this feature and its real use-cases. As I understand it, @rhdunn took inspiration from fn:concat. I want to try and make the argument that in my opinion
|
Treating an empty sequence as a zero-length string is something that all the XPath 1.0 string-handling functions do, and it makes eminent sense in the case of concat(), though I'm less comfortable with it elsewhere (e.g. substring()). You also seem to be commenting on the fact that concat() was defined to take two-or-more strings rather than zero-or-more. I remember we had a debate about that during the 2.0 development and it was a classic "orthogonality is good" versus "is there a use case?" debate. |
The MotivationThe motivation for this was based on
Thinking in terms of annotations, there are also annotations that take variadic arguments:
The motivation then is to be able to specify these using valid XPath/XQuery syntax. Requirements for a Variadic Argument Proposal
The DesignFor the design, I took inspiration from Java that maps the variable arguments into an object array. The variadic arguments needed to support nested sequences at a given parameter, and supply an optional type for the variadic parameters. This is how I ended up with the syntax I proposed. |
As for the general question, is a general varargs mechanism useful, I think the answer is probably yes, so long as it can be done very cleanly. concat() is a kludge; eliminating the kludge would be good; extending the kludge so that it affects more of the language would not be good. |
@michaelhkay Unless I missed something, As each of those arguments can be an empty sequence, it seems to me that |
@rhdunn I appreciate you restating your motivation and your requirements, I think that is helpful :-) I am a little uneasy about pointing to annotations as an example for variadic parameters though. Annotations don't have parameters, rather they have explicit literal values given in a list. |
@adamretter, the There's no logical reason why it should not allow 0 arguments or 1 argument, but the WG decided not to allow that for lack of a use case. Which might have been a reasonable argument at the time, but feels flawed now that we have constructs like |
@adamretter I'm happy to defer the specification of variadics on annotations -- I was thinking about it in terms of my annotation declaration proposal, but we could talk about them in that proposal. |
I think I would go for the following design:
|
I like the idea of allowing
which could be replaced with (at least if the default values are always
|
So I like the idea, but I would request different syntax. I think things like |
@adamretter What about something inspired from C++ then, like the following?
|
My only reservation about this is that it feels as if How about |
+1. I liked the (potential) idea of making the last parameter optional via |
@michaelhkay Sounds good. The space would then not be mandatory per se, but only as a consequence of having 2 different tokens out of the lexer (which would not be the case in case of |
I have updated the proposal to reflect the proposed |
…the function arity; add an ArrowFunctionSpecifier example.
This describes the variable function arguments syntax extension discussed on the expath group.