Skip to content
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

Proposal to support optional parameter values on static functions. #155

Closed
rhdunn opened this issue Sep 27, 2022 · 4 comments
Closed

Proposal to support optional parameter values on static functions. #155

rhdunn opened this issue Sep 27, 2022 · 4 comments
Labels
Feature A change that introduces a new feature XPath An issue related to XPath

Comments

@rhdunn
Copy link
Contributor

rhdunn commented Sep 27, 2022

This proposal allows users to define optional parameters for a function. When calling the function statically (e.g. f(1,2,3)), or creating a named function reference to it (e.g. f#3), any optional parameters that are not specified take the default values.

Motivation

Defining functions that can take one or more optional argument (such as in the Functions and Operators specification) adds a lot of boilerplate code. For example, `fn:tokenize could be written as:

declare function fn:tokenize($value as xs:string?) as xs:string* {
   fn:tokenize($value, "\s+")
};

declare function fn:tokenize($value as xs:string?, $pattern as xs:string) as xs:string* {
   fn:tokenize($value, $pattern, "")
};

declare function fn:tokenize($value as xs:string?, $pattern as xs:string, $flags as xs:string) as xs:string* {
   ...
};

In this proposal, the above can be written more simply and concisely as:

declare function fn:tokenize(
   $value as xs:string?,
   $pattern as xs:string := "\s+",
   $flags as xs:string := ""
) as xs:string* {
   ...
};

Both of these definitions of fn:tokenize are equivalent.

Note

Once this proposal has been accepted, we should go through the XSLT and XQFO specifications and use defaults for all the relevant functions.

@rhdunn rhdunn added XQuery An issue related to XQuery Feature A change that introduces a new feature labels Sep 27, 2022
@rhdunn rhdunn changed the title Proposal to support optional parameter values Proposal to support optional parameter values on static functions Sep 27, 2022
@rhdunn rhdunn added XPath An issue related to XPath and removed XQuery An issue related to XQuery labels Sep 27, 2022
@rhdunn rhdunn changed the title Proposal to support optional parameter values on static functions Proposal to support optional parameter values on static functions. Sep 27, 2022
@michaelhkay
Copy link
Contributor

This is a good start, but the proposal is far from complete. Giving an example of old syntax and an example of new syntax and stating that they are equivalent does not constitute a specification. In addition, defining the semantics of the default value using this approach has problems. It would prevent the use of a default such as ".", because "." is undefined within a function body. If we want to allow that, then specifying the semantics of a variadic function declaration in terms of a lexical expansion to a set of non-variadic function declarations isn't going to work.

There's a lot of prose in the spec that presumes a one-to-one correspondence between function declarations and functions in the static context, and therefore a lot of editorial work to handle the fact that this can no longer be assumed.

@rhdunn
Copy link
Contributor Author

rhdunn commented Sep 28, 2022

I wanted to specifically only define the problem initially, not go into the specifics. That is, this is a high-level view of what is to be achieved. Think of this draft as being from the user's perspective.

@michaelhkay
Copy link
Contributor

A pull request to implement this for XSLT and XQuery has been submitted.

@michaelhkay
Copy link
Contributor

I believe this can now be closed as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature A change that introduces a new feature XPath An issue related to XPath
Projects
None yet
Development

No branches or pull requests

2 participants