RE: No convenient way to cast one expression to the same type as another?

Hmm, then this is a place where XQuery defines semantics that cannot be 
expressed using XQuery.

For example, suppose you want to write a user-defined function that implements 
a general comparison operator (such as =) using only value comparison 
operators.  You'll end up with:

define function my:equals($lhs, $rhs) as xs:boolean {
  some $i in fn:data($lhs), $j in fn:data($rhs)
  satisfies promote-general($i, $j) eq my:promote-general($j, $i)
}

where my:promote-general promotes the two operands to a common type using the 
rules XQuery has defined for the general comparisons:

define function my:promote-general($expr, $type) {
    typeswitch ($expr)
      case xdt:untypedAtomic return
        typeswitch($type)
          case xs:decimal        return cast as xs:double($expr)
          case xs:double         return cast as xs:double($expr)
          case xs:float          return cast as xs:double($expr)
          case xdt:untypedAtomic return cast as xs:string($expr)
          default                return my:same-type($expr, $type)
      default return $expr
}

but then XQuery provides no way to implement the my:same-type function, even 
though this is the behavior it has defined for the general comparisons (i.e., 
implementations do have to implement this kind of type conversion).

I think this is a real loss of generality in the language.  Either the 
definitions of the general comparisons should be simplified so that they can 
be implemented in terms of the value comparisons, or else the missing type 
conversion functionality should be added.
> 
> > Given two opaque expressions $x and $y, does XQuery have a convenient 
> > expression that converts $x to the same (dynamic or static) 
> > type as $y?
> 
> No, I don't think it does. I think we should consider this when and if we
> provide instrospection (types as first class objects) in a future version of
> the language.
> 
> Michael Kay
> 
> 
> > 
> > I can write a gnarly (60+ lines, just to handle singletons) 
> > typeswitch that 
> > converts $x to the dynamic type of $y, provided I'm willing 
> > to settle for the 
> > built-in XQuery types only.  I haven't found any way to 
> > handle user-defined 
> > types, or to convert $x to the static type of $y.
> > 
> > Strangely, this operation does come up (at least for dynamic 
> > types); for 
> > example, the general comparison operators perform this conversion on 
> > xdt:untypedAtomic values in some cases.
> > 
> > Perhaps there should be a built-in function like 
> > convert-to-same-type-as($exprToConvert as item*, $type as 
> > item*) as item* ?
> > 
> > Cheers,
> > 

> > Michael Brundage
> > Writing as
> > Author, "XQuery: The XML Query Language" (Addison-Wesley, to 
> > appear 2003) Co-author, "Professional XML Databases" (Wrox 
> > Press, 2000)
> > 
> > not as
> > Technical Lead
> > Common Query Runtime/XML Query Processing
> > WebData XML Team
> > Microsoft
> > 
> 

Received on Tuesday, 10 June 2003 00:49:52 UTC