#185 typeswitch produced compile-time type error

Michael Kay

In XQuery, a typeswitch expression gives a compile-time
type error if it's known at compile time that one of
the branches will not be executed.

For example this happens with the expression:

let $v := 1.5
typeswitch ($v)
case $v1 as xs:integer return 'integer'
case $v2 as xs:decimal return 'decimal'
default return 'default'

The fix to this is to change the analyze() method in
net.sf.saxon.IfExpression to:

public Expression analyze(StaticContext env) throws

XPathException {
condition = condition.analyze(env);
if (condition instanceof BooleanValue) {
if (((BooleanValue)condition).getValue()) {
return thenExp.analyze(env);
} else {
return elseExp.analyze(env);
} else {
thenExp = thenExp.analyze(env);
elseExp = elseExp.analyze(env);
return simplify();

Note: A typeswitch expression is compiled to a series
of nested IfExpressions. In the above example the
compiled code is

let $v := 1.5 return
if ($v instance of xs:integer)
then let $v1 as xs:integer := $v return "integer"
else if ($v instanceof xs:decimal)
then let $v2 as xs:decimal := $v return "decimal"
else "default"

This expression fails with a type error because $v is
not an integer, despite the fact that it's in a branch
that won't be executed.

Michael Kay


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks