Syntactical type checking
This functionality does not run in MATLAB.
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.
See the Algorithms section below for details on the overloading mechanism.
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:
The number 7 matches other types as well:
testtype(7, Type::PosInt), testtype(7, Type::Prime), testtype(7, Type::Numeric), testtype(7, Type::Odd)
Subtypes of expressions can be specified via character strings:
testtype(sin(x), "function"), testtype(sin(x), "sin"), testtype(sin(x), "cos")
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)
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.