Product SiteDocumentation Site

11.5. Typeswitch Expressions

A typeswitch expressions tests if the value resulting from the first operand matches a given list of types. The expression corresponding to the first matching case is finally evaluated. If there is no match, the expression in the default clause is evaluated.
Example 11.7. Typeswitch expression.
typeswitch("foo")
  case integer return "integer"
  case string return "string"
  case object return "object"
  default return "other"
Results:
"string"

In each clause, it is possible to bind the value of the first operand to a variable.
Example 11.8. Typeswitch expression.
typeswitch("foo")
  case $i as integer return $i + 1
  case $s as string return $s || "foo"
  case $o as object return [ $o ]
  default $d return $d
Results:
"foofoo"

The vertical bar can be used to allow several types in the same case clause.
Example 11.9. Typeswitch expression.
typeswitch("foo")
  case $a as integer | string
      return { "integer or string" : $a }
  case $o as object
      return [ $o ]
  default $d
      return $d
Results:
{
  "integer or string" : "foo"
}