Comments (6)
Is there any reason not to allow the default value to be any expression? The semantics are that the expression is evaluated in the dynamic context of the function call. This allows the default value for $collation functions such as fn:compare()
to be expressed as $collation := fn:default-collation()
.
Note that XSLT allows a named template to be defined with optional parameters (for example it allows <xsl:param name="collation" select="default-collation()"/>
and in XSLT we would want to use the same syntax and semantics for functions if at all possible.
The static context, of course, doesn't include the local variables of the caller. But (in the XSLT case) it does include global variables.
from qtspecs.
The following table lists the default values for the various parameters of the built-in functions. Where the "Changed?" column has a value of "yes", the parameter and function need signature and logic changes to support the default value; these changes are described in separate issues (#69 and #70).
Using the default values means that the combined functions depend on all the context items that the currently separate versions do. That is, if the 2-argument version depends on the default collation, but the 3-argument version does not, then with the default values applied both versions depend on the default collation.
Function | Parameter | Default Value | Changed? |
---|---|---|---|
array:sort |
$collation |
() |
no |
array:sort |
$key |
fn:data#1 |
no |
array:subarray |
$length |
() |
yes |
map:merge |
$options |
n/a [1] | no |
fn:adjust-date-to-timezone |
$timezone |
() |
no |
fn:adjust-dateTime-to-timezone |
$timezone |
() |
no |
fn:adjust-time-to-timezone |
$timezone |
() |
no |
fn:analyze-string |
$flags |
"" |
no |
fn:base-uri |
$node |
. [2] |
no |
fn:collation-key |
$collation |
() |
yes |
fn:collection |
$uri |
() |
no |
fn:compare |
$collation |
() |
yes |
fn:contains |
$collation |
() |
yes |
fn:contains-token |
$collation |
() |
yes |
fn:copy-of [XSLT] |
$input |
. [2] |
no |
fn:data |
$input |
. [2] |
no |
fn:deep-equal |
$collation |
() |
yes |
fn:differences |
$collation |
() |
yes |
fn:differences |
$options |
map {} [3] |
yes |
fn:distinct-values |
$collation |
() |
yes |
fn:document [XSLT] |
$base-node |
() |
yes |
fn:document-uri |
$node |
. [2] |
no |
fn:element-with-id |
$node |
. [2] |
no |
fn:ends-with |
$collation |
() |
yes |
fn:error |
$code |
() |
no |
fn:error |
$description |
[4] | no |
fn:error |
$error-object |
[4] | no |
fn:format-date |
$language |
() [5] |
no |
fn:format-date |
$calendar |
() [5] |
no |
fn:format-date |
$place |
() [5] |
no |
fn:format-dateTime |
$language |
() [5] |
no |
fn:format-dateTime |
$calendar |
() [5] |
no |
fn:format-dateTime |
$place |
() [5] |
no |
fn:format-integer |
$language |
() |
no |
fn:format-number |
$decimal-format-name |
() |
no |
fn:format-time |
$language |
() [5] |
no |
fn:format-time |
$calendar |
() [5] |
no |
fn:format-time |
$place |
() [5] |
no |
fn:function-available [XSLT] |
$arity |
() |
yes |
fn:generate-id |
$node |
. [2] |
no |
fn:has-children |
$node |
. [2] |
no |
fn:highest |
$collation |
() |
no |
fn:highest |
$key |
fn:data#1 |
no |
fn:id |
$node |
. [2] |
no |
fn:idref |
$node |
. [2] |
no |
fn:index-of |
$collation |
() |
yes |
fn:json |
$options |
n/a [1] | no |
fn:json-doc |
$options |
n/a [1] | no |
fn:json-to-xml |
$options |
n/a [1] | no |
fn:key [XSLT] |
$top |
/ [2] |
no |
fn:lang |
$node |
. [2] |
no |
fn:load-xquery-module |
$options |
n/a [1] | no |
fn:local-name |
$node |
. [2] |
no |
fn:lowest |
$collation |
() |
no |
fn:lowest |
$key |
fn:data#1 |
no |
fn:matches |
$flags |
"" |
no |
fn:max |
$collation |
() |
yes |
fn:min |
$collation |
() |
yes |
fn:name |
$node |
. [2] |
no |
fn:namespace-uri |
$node |
. [2] |
no |
fn:nilled |
$node |
. [2] |
no |
fn:node-name |
$node |
. [2] |
no |
fn:normalize-space |
$value |
. [2] |
no |
fn:normalize-unicode |
$form |
"NFC" |
no |
fn:number |
$node |
. [2] |
no |
fn:parse-json |
$options |
n/a [1] | no |
fn:path |
$node |
. [2] |
no |
fn:random-number-generator |
$seed |
() |
no |
fn:replace |
$flags |
() [6] |
yes |
fn:replace |
$action |
() |
no |
fn:resolve-uri |
$base |
() |
yes |
fn:round |
$precision |
0 |
no |
fn:round-half-to-even |
$precision |
0 |
no |
fn:serialize |
$params |
() |
no |
fn:sort |
$collation |
() |
no |
fn:sort |
$key |
fn:data#1 |
no |
fn:starts-with |
$collation |
() |
yes |
fn:string |
$item |
. [2] |
no |
fn:string-join |
$separator |
"" |
no |
fn:string-length |
$value |
fn:string(.) [2] |
no |
fn:subsequence |
$length |
() |
yes |
fn:substring |
$length |
() |
yes |
fn:substring-after |
$collation |
() |
yes |
fn:substring-before |
$collation |
() |
yes |
fn:sum |
$zero |
0 [7] |
no |
fn:tokenize |
$pattern |
() |
yes |
fn:tokenize |
$flags |
"" |
no |
fn:trace |
$label |
[4] | no |
fn:uniform |
$collation |
() |
yes |
fn:unique |
$collation |
() |
yes |
fn:unparsed-entity-public-id [XSLT] |
$doc |
. [2] |
no |
fn:unparsed-entity-uri [XSLT] |
$doc |
. [2] |
no |
fn:unparsed-text |
$encoding |
() |
yes |
fn:unparsed-text-available |
$encoding |
() |
yes |
fn:unparsed-text-lines |
$encoding |
() |
yes |
fn:uri-collection |
$uri |
() |
no |
fn:xml-to-json |
$options |
n/a [1] | no |
[1] The function is map-variadic, so those rules will supply the default map {}
value.
[2] The default value in these cases cannot be evaluated statically, but require passing the context item at the point at which the function call is evaluated.
[3] The default is specified here as the $collation
parameter also has a default value, so the following parameters should also have specified default values.
[4] The $description, $error-object, and $label arguments have implementation-defined default values. As such, the 0- and 1-argument versions of fn:error
can be combined, but the 2- and 3-argument versions cannot, nor can fn:trace
.
[5] The function gains additional 3- and 4-argument versions due to it having default values for the last 3 parameters.
[6] By using the 5-argument version with default arguments, the $flags argument will be optional in the 4-argument case, as it is in the 5-argument version. As $flags is optional in the 5-argument version, it does not need changing to support default values.
[7] The 1-argument version of the function will have a return type of xs:anyAtomicType?
when the default value is used in the 2-argument version.
from qtspecs.
Given the above analysis, and having experimented with optional parameters, there are two possibilities for defining the values for the optional arguments:
- keep the
ExprSingle
as part of the above grammar, and define an error if the result is not a constant value,.
, or/
(or possibly extended to any path expression); - define a
ConstExprSingle
symbol (and others as necessary) to limit the result to constant values (numeric and string literals, named function references, empty sequences, and empty or constant maps),.
, or/
.
Evaluating Default Values
-
Default values are evaluated using the static context of the declaration where the default value is specified. This means that statically-known namespaces, default element and type namespaces, statically-known functions, etc. are evaluated from the function declaration. The exceptions to this are the context item and focus, which are deferred to when the function is called.
-
If the default value of the parameter is
.
, the function is ·context-dependent· and ·focus-dependent· when no value is passed to that parameter. The context item is determined at the point at which the function is called.
Example:
If the function is called as a step in a path expression, the context item is the context item created during the evaluation of the previous step. The context item is the inner focus context item at the point of the function call as described in section 4.6.1.1 Path operator (/).
- If the default value of the parameter is a path expression, the function is ·context-dependent· and ·focus-dependent· when no value is passed to that parameter. The value of the expression is the result of the path expression using the current context item as the initial step context.
Example:
Using
/
as the default value uses the root of the document bound to the context item at the point at which the function is called.
- Otherwise, if the default value is not a statically known value, an XPST#### error is raised.
from qtspecs.
I wonder if it makes sense to restrict this to only defining and allowing constant value expressions, and then having separate issues for how to handle the .
and /
default values.
from qtspecs.
Here are some possible designs to handle .
and /
. Note: I'm in favour of design 2.
Design 1
A possible way for supporting .
and /
would be to use := ->{ . }
and := ->{ / }
respectively. For this to work, we would need the following new behaviour...
- If the default value of a parameter is an inline function, that function must have 0 parameters. The static context of that function is the current static context.
- When evaluating a static function call, if the default value is a 0-arity function then that function is evaluated at this point with the static context of the caller.
Note: The way this is defined would mean that you can't pass a 0-arity function reference as a default parameter.
Design 2
Another possibility is to use a variant of design 1, but define a DefaultExpr
:
OptionalParam ::= Param ":=" OptionalParamValue
OptionalParamValue ::= EmptySequence | Literal | NamedFunctionRef | DefaultExpr
EmptySequence := "(" ")"
DefaultExpr := "default" EnclosedExpr
Here, DefaultExpr
would allow := default { . }
for context and focus dependent functions, and := default { / }
for fn:key
.
The semantics of DefaultExpr
would be:
- You can only use
DefaultExpr
within the definition of anOptionalParam
(as restricted by the grammar). - If the default value of a parameter is a default expression, the static context of that expression is the current static context when statically resolving the definitions within that expression (i.e. functions, variables, etc.). If a default expression contains a
.
, path expression, or anything else that depends on the dynamic context, then that evaluation is deferred (see Evaluating Static Function Calls). - In "Evaluating Static Function Calls", if an optional parameter value is a default expression then that expression is evaluated using the dynamic context of the caller, and the result is bound to that parameter.
from qtspecs.
I'm happy with that.
So to confirm, := .
will evaluate the context item at the point where the function is called (Evaluating Static Function Calls), likewise for := /
?
If that is the case, we can just have:
OptionalParam ::= Param ":=" ExprSingle
Otherwise, we would need the DefaultExpr defined above:
OptionalParam ::= Param ":=" ( ExprSingle | DefaultExpr )
DefaultExpr := "default" EnclosedExpr
from qtspecs.
Related Issues (20)
- Canonical serialization HOT 3
- Remove fn:numeric-compare HOT 1
- Coercion rules: implicit types
- Module import: apparent contradiction HOT 2
- fn:iterate-while → fn:while-do, fn:do-until
- fn:scan-left and fn:scan-right - produce accumulation of results HOT 10
- Partial Function Applications: Allow return of function name HOT 5
- Parameters with default values: fn:lang, fn:id, fn:idref, fn:element-with-id
- Establish a default value for the XSLT fixed-namespaces attribute HOT 12
- Options parameters as record types HOT 9
- Milliseconds ↔ xs:dayTimeDuration, Unix time ↔ xs:dateTime HOT 4
- Should ??KS flatten the results HOT 4
- Simulating Objects: Performance HOT 64
- Errors in forming function items (continued) HOT 4
- fn:has-attributes HOT 5
- Rewrite spec of deep lookup operator: edits
- XPath Appendix I: Comparisons
- XQFO: Context item → value
- fn:parse-json, fn:json-to-xml: `number-parser`, `fallback`
- Rules for context-dependent function references in XSLT (e.g. regex-group#1) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qtspecs.