Documentation

# `testtype`

Syntactical type checking

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```testtype(`object`, `T`)
```

## Description

`testtype(object, T)` checks whether the object is syntactically of type `T`.

The type object `T` may be either a domain type such as `DOM_INT`, `DOM_EXPR` etc., a string as returned by the function `type`, or a Type object. The latter are probably the most useful predefined values for the argument `T`.

### Note

`testtype` performs a purely syntactical check. Use `is` for semantical checks taking into account properties of identifiers!

## Examples

### Example 1

The following call tests, whether the first argument is an expression. Expressions are basic objects of domain type `DOM_EXPR`:

`testtype(x + y, DOM_EXPR)`

The `type` function distinguishes expressions. The corresponding type string is a valid type object for `testtype`:

`type(x + y), testtype(x + y, "_plus")`

The following call tests, whether the first argument is an integer by querying, whether it is of domain type `DOM_INT`:

`testtype(7, DOM_INT)`

Note that `testtype` performs a purely syntactical test. Mathematically, the integer 7 is a rational number. However, the domain type `DOM_RAT` does not encompass `DOM_INT`:

`testtype(7, DOM_RAT)`

The Type library provides more flexible type objects. E.g., `Type::Rational` represents the union of `DOM_INT` and `DOM_RAT`:

`testtype(7, Type::Rational)`

The number 7 matches other types as well:

```testtype(7, Type::PosInt), testtype(7, Type::Prime), testtype(7, Type::Numeric), testtype(7, Type::Odd)```

### Example 2

Subtypes of expressions can be specified via character strings:

`type(f(x)), type(sin(x))`

```testtype(sin(x), "function"), testtype(sin(x), "sin"), testtype(sin(x), "cos")```

### Example 3

We demonstrate how to implement a customized type object “`div3`” which is to represent integer multiples of 3. One has to create a new domain with a “`testtypeDom`attribute:

```div3 := newDomain("divisible by 3?"): div3::testtypeDom := x -> testtype(x/3, Type::Integer):```

Via overloading, the command `testtype(object, div3)` calls this slot:

`testtype(5, div3), testtype(6, div3), testtype(sin(1), div3)`

`delete div3:`

## Parameters

 `object` Any MuPAD® object `T` A type object

## Return Values

`TRUE` or `FALSE`.

`object`, ` T`

## Algorithms

Overloading of `testtype` works as follows: First, it is checked whether `domtype(object) = T` or ```type(object) = T``` holds. If so, `testtype` returns `TRUE`.

Next, the method `"testtype"` of the domain `object::dom` is called with the arguments `object, T`. If this method returns a result other than `FAIL`, then `testtype` returns this value.

If the method `object::dom::testtype` does not exist or if this method returns `FAIL`, then overloading by the second argument is used:

• If `T` is a domain, then the method `"testtypeDom"` of `T` is called with the arguments `object, T`.

• If `T` is not a domain, then the method `"testtypeDom"` of `T::dom` is called with the arguments `object, T`.