Previous Page Next Page Contents

Type::Singleton -- type to identify exactly one object

Introduction

testtype(x, Type::Singleton(y)) is equivalent to bool(x = y).

Call(s)

testtype(obj, Type::Singleton(t_obj))

Parameters

obj - any MuPAD object
t_obj - any object to identify

Returns

see testtype

Related Functions

_equal, bool, testtype, Type::Union

Details

Example 1

Check, if x is really x:

>> testtype(x, Type::Singleton(x))
                                   TRUE

But the next call does the same:

>> bool(x = x)
                                   TRUE

Example 2

Type::Singleton exists to create special testing expressions:

>> T := Type::Union(Type::Singleton(hold(All)), Type::Constant):

With the type T the option All and any constant can be identified with one call of testtype:

>> testtype(4, T), testtype(hold(All), T), testtype(x, T)
                             TRUE, TRUE, FALSE

But (e.g., in procedures) the following example works faster:

>> test := X -> testtype(X, Type::Constant) or bool(X = hold(All)):
   test(4), test(hold(All)), test(x)
                             TRUE, TRUE, FALSE

One way to test a list of options for syntactical correctness is the following:

>> T := Type::Union(
          // Name = "..."
          Type::Equation(Type::Singleton(hold(Name)), DOM_STRING),
          // Mode = n, n in {1, 2, 3}
          Type::Equation(Type::Singleton(hold(Mode)), 
                         Type::Interval([1,3], Type::Integer)),
          // Quiet
          Type::Singleton(hold(Quiet))
        ):
>> testtype((Name = "abcde", Quiet), Type::SequenceOf(T))
                                   TRUE

We only allow the values 1, 2, and 3 for Mode, however:

>> testtype((Quiet, Mode = 0), Type::SequenceOf(T))
                                   FALSE

Obviously, it would be a good idea to tell the user which options we could not grok:

>> error("Unknown option(s): ".expr2text(
           select((Quiet, Mode = 0), 
                  not testtype, Type::SequenceOf(T))))
      Error: Unknown option(s): Mode = 0
>> delete T, test:

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000