testtype -- syntactical type
checking
Introductiontesttype(object, T) checks whether the
object is syntactically of type T.
Call(s)testtype(object, T)
Parametersobject |
- | any MuPAD object |
T |
- | a type object |
ReturnsTRUE or FALSE.
object, T
Related
Functionscoerce, domtype, hastype, is, type, Type
DetailsT 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 pre-defined values for the argument
T.testtype performs a purely syntactical
check. Use is for
semantical checks taking into account properties of identifiers!
testtype is a function of the system kernel.
Example
1The following call tests, whether the first argument is
an expression. Expressions are basic objects of domain type DOM_EXPR:
>> testtype(x + y, DOM_EXPR)
TRUE
The type
function distinguishes expressions. The corresponding type string is a
valid type object for testtype:
>> type(x + y), testtype(x + y, "_plus")
"_plus", TRUE
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)
TRUE
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)
FALSE
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)
TRUE
The number 7 matches other types as well:
>> testtype(7, Type::PosInt), testtype(7, Type::Prime), testtype(7, Type::Numeric), testtype(7, Type::Odd)
TRUE, TRUE, TRUE, TRUE
Example
2Subtypes of expressions can be specified via character strings:
>> type(f(x)), type(sin(x))
"function", "sin"
>> testtype(sin(x), "function"), testtype(sin(x), "sin"), testtype(sin(x), "cos")
TRUE, TRUE, FALSE
Example
3We 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
``testtype'' attribute:
>> div3 := newDomain("divisible by 3?"):
div3 := slot(div3, "testtype",
proc(x) begin
return(testtype(x/3, Type::Integer))
end_proc):
Via overloading, the command testtype(object,
div3) calls this slot:
>> testtype(5, div3), testtype(6, div3), testtype(sin(1), div3)
FALSE, TRUE, FALSE
>> delete div3:
Backgroundtesttype works as follows: First, it is
checked whether domtype(object) = T or type(object)
= T holds. If so, testtype returns
TRUE."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.object::dom::testtype does not exist or
if this method returns FAIL, then overloading by the
second argument is used:
T is a domain, then the method
"testtype" of T is called with the arguments
object, T.T is not a domain, then the method
"testtype" of T::dom is called with the
arguments object, T.