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

Adopt the coercion rules for variables in XQuery #189

Closed
michaelhkay opened this issue Oct 7, 2022 · 4 comments
Closed

Adopt the coercion rules for variables in XQuery #189

michaelhkay opened this issue Oct 7, 2022 · 4 comments
Labels
Enhancement A change or improvement to an existing feature XQuery An issue related to XQuery

Comments

@michaelhkay
Copy link
Contributor

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 write let $x as xs:decimal := xs:decimal(@version). (Or if you find that annoying, you can write let $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.

@ChristianGruen
Copy link
Contributor

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.

@dnovatchev
Copy link
Contributor

dnovatchev commented Oct 7, 2022

@michaelhkay,
Will the new rule apply to expressions such as:

let $x   :=  /*/@version
  return
     $x  treat as xs:decimal

Can we say explicitly that this proposal will also apply for XPath?

@michaelhkay
Copy link
Contributor Author

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: $x treat as T essentially means assert $x instance of T and I don't propose to change that.

@ChristianGruen ChristianGruen added XQuery An issue related to XQuery Enhancement A change or improvement to an existing feature labels Oct 10, 2022
@michaelhkay
Copy link
Contributor Author

Completed by the adoption of PR #254

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement A change or improvement to an existing feature XQuery An issue related to XQuery
Projects
None yet
Development

No branches or pull requests

3 participants