Pref::typeCheck

Type checking of formal parameters

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

Pref::typeCheck(Always | Interactive | None)
Pref::typeCheck(NIL)
Pref::typeCheck()

Description

Pref::typeCheck determines the kind of type checking of procedure parameters.

The definition of a MuPAD® procedure may contain formal parameters. There is a syntax to attach a type specification to these parameters. If and when type checking is enabled, the types of actual parameters are checked against the type specifications and an error is raised if a parameter does not meet the specification.

Type specifications are used as the second parameter of testtype. The most important ones are Domain Types and objects of the domain Type. With Type, user defined types can be easily added to the system to extend the type checking mechanism.

The arguments of Pref::typeCheck can be:

  • None

    No parameters are checked.

  • Interactive

    Parameters entered interactively are checked. This is the default.

  • Always

    All formal parameters are checked.

The default value Interactive means: When the user is calling a procedure f, its parameters will be checked, but none of the procedures called by the user called procedure f performs type checking.

A call of Pref::typeCheck without arguments returns the current value. The argument NIL resets the default value, which is Interactive.

Examples

Example 1

We define a procedure f expecting an identifier and an integer:

f:= proc(a : DOM_IDENT, b : DOM_INT)
    begin
      evalassign(a, b, 1)
    end_proc:
f(a, 2)

Now a has the value 2, but an identifier is expected:

f(a, 2)
Error: Invalid object '2'. Type of argument number 1 must be 'DOM_IDENT'.
  Evaluating: f
delete a:

Options

Always

Parameter types are checked for every call.

Interactive

Parameter types are checked for interactive calls, not for “inner” calls during the computation.

None

No parameter type checks are performed by the MuPAD kernel. Explicit checks inside procedures still use the usual testargs mechanism.

Return Values

Previously defined value

Algorithms

The syntax to test parameters directly (without a test in the procedure body) is the formal parameter followed by a colon and then the type object: proc(a : DOM_IDENT, b : Type::Integer). This means: a must be of the type DOM_IDENT and b must be of the type Type::Integer.

Note that you cannot use automatic type checking for arguments that are used for overloading inside the procedure.

The objects of the Type library are usually more general than the MuPAD kernel types.

See Also

MuPAD Domains

MuPAD Functions