Previous Page Next Page Contents

testtype -- syntactical type checking

Introduction

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

Call(s)

testtype(object, T)

Parameters

object - any MuPAD object
T - a type object

Returns

TRUE or FALSE.

Overloadable:

object, T

Related Functions

coerce, domtype, hastype, is, type, Type

Details

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)
                                   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 2

Subtypes 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 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 ``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:
    

Background

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000