-
Notifications
You must be signed in to change notification settings - Fork 15
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
Adopt the coercion rules for variables in XQuery #189
Comments
I remember the pain to teach the difference between treating, promoting, casting & coercing, and I never understood why function arguments and variable bindings are handled differently, so I’m strongly in favor of getting the behavior unified. |
@michaelhkay, let $x := /*/@version
return
$x treat as xs:decimal Can we say explicitly that this proposal will also apply for XPath? |
The proposal doesn't currently affect XPath, because XPath doesn't allow type declarations on variables. But if we made this change to XQuery, it would make it easier to migrate type declarations on let expressions into XPath. Generally I think we should try to support a larger subset of FLWOR expression syntax in XPath, though I'm reluctant to add the features such as order by and group by that can't be expressed as nested loops. "treat as" isn't affected at all: |
Completed by the adoption of PR #254 |
Currently XSLT applies the coercion rules (formerly the function conversion rules) both when supplying values to arguments of a function declaring a required type, and when binding values to variables declaring a required type. This is pretty much essential because it allows xsl:variable and xsl:param to have the same semantics.
XQuery applies the coercion rules only for function arguments, and not for variable bindings. For variables, the supplied value must already be a value of the required type. For example, you can't write
let $x as xs:decimal := @version
, you have to writelet $x as xs:decimal := xs:decimal(@version)
. (Or if you find that annoying, you can writelet $x as xs:decimal := +@version
, because unary plus invokes coercion).This is the main reason declared types are not allowed in XPath: the XSLT community felt that if they were allowed, the rules had to be the same as the rules for XSLT variable bindings. We could eliminate this objection if XQuery adopted the coercion rules for variable bindings.
I have some hesitation in proposing this, because when I proposed it before, it was quite strongly opposed by some of the original XQuery designers, whose views I respect. But although they argued their case strongly, I confess I did not understand their position. I simply see no reason why assignments to function parameters and assignments to variables should be handled differently. It works in XSLT, and makes the language more usable, and I see no reason why it should not work in XQuery.
It's not uncommon for languages to impose different rules for the two cases. Java, for example has a variety of invocation contexts (strict and loose), and variable assignment allows conversions such as narrowing of integer constants that are not allowed on method calls (interestingly, Java allows more conversions on variable assignment than on method calling). In XQuery, however, I can see no rationale for the rules being different.
We're proposing syntax for XQuery (function calls with keyword parameters) that make parameter assignment look more like variable assignment. I think we should match this by making them semantically more similar.
The text was updated successfully, but these errors were encountered: