Variables

A variable may be bound by a let clause in a FLWOR expression. The type of the variable is the same as the type of its defining expression. Here are some examples:

let $z := 1+2 return $z+$z has type xs:integer
let $z := 1e0+1.0 return $z+$z has type xs:double

In the first let expression, the expression 1+2 has type xs:integer; therefore, the variable $z also has type xs:integer, and the return expression is typed assuming that $z has type xs:integer. Similarly, in the second expression, variable $z has type xs:double. The static type system keeps track of the types of variables in the static environment; whenever a variable is bound, the static environment is extended by associating the variable with the computed type.

The type of a variable may also be declared explicitly. In this case, a static type error is raised if the type of the defining expression is not a subtype of the declared type. Here are some examples:

let $x as xs:integer := 1+2 return $x+$x has type xs:integer
let $x as xs:decimal := 1+2 return $x+$x has type xs:decimal

The second example is well typed, because xs:integer is a subtype of xs:decimal.

One type is a subtype of another if every value that matches the first type always matches the second type. In particular, whenever one type is derived from a second type by restriction, the first type is a subtype of the second. So xs:integer, for example, is a subtype of xs:decimal, and the type NewUser defined earlier is a subtype of the type User.

Subtyping does not include type promotion. So xs:integer is not a subtype of xs:double, even though it is beneath it in the promotion hierarchy.

let $x as xs:double := 1+2 return $x+$x is a static type error
					

One can specify promotion by including an explicit type coercion, with or without a type declaration.

let $x := xs:double(1+2) return $x+$x has type xs:double
let $x as xs:double := xs:double(1+2) return $x+$x has type xs:double

The type of an expression depends only on the type of its subexpressions. Thus, a static type error will be raised whenever the type of the defining expression is not a subtype of the declared type, even if the value belongs to the declared type. The let expression defining variable $y below is an example of such a static type error:

let $x as xs:decimal := 1,
    $y as xs:integer := $x+$x
return $y+$y               

Even though the value of $x is an integer at run-time, the type of $x is decimal for the purposes of static analysis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.209