testargs
-- decide whether
procedure arguments should be testedInside a procedure, testargs
indicates whether the
procedure was called on the interactive level.
testargs()
testargs(b)
b |
- | TRUE or
FALSE |
proc
, testtype
, Pref::typeCheck
If a procedure is called on the interactive level, i.e., if its parameters are supplied interactively by the user, then the parameters should be checked. If the input parameters do not comply with the documented specification of the procedure, then appropriate error messages should be returned to notify the user of wrong usage.
If the procedure is called by another procedure, then no check of the parameters should be performed to improve efficiency. The calling procedure is supposed to make sure that appropriate parameters are passed.
testargs
is the tool to check whether the arguments
should be tested: called inside the body of a procedure,
testargs(
)
returns TRUE
if the
procedure was called on the interactive level. Otherwise, it returns
FALSE
.
testargs
has two modes. In the ``standard mode'', its
functionality is as described above. In the ``debugging mode'', the
call testargs(
)
always returns
TRUE
. This supports the debugging of procedures: any
function using testargs
checks its parameters and returns
useful error messages if called in an inappropriate way.
The call testargs(
TRUE)
switches to the
``debugging mode'', i.e., parameter testing is switched on
globally.
The call testargs(
FALSE)
switches to the
``standard mode'', i.e., parameter testing is used only on the
interactive level.
The call testargs(
b)
returns the
previously set value.
Pref::typeCheck
.testargs
is a function of the system kernel.The following example demonstrates how
testargs
should be used inside a procedure. The function
p
is to generate a sequence of n
zeroes; its
argument should be a positive integer:
>> p := proc(n) begin if testargs() then if not testtype(n, Type::PosInt) then error("expecting a positive integer"); end_if; end_if; return(0 $ n) end_proc:
Its argument is checked when p
is called on
the interactive level:
>> p(13/2)
Error: expecting a positive integer [p]
Calling p
from within a procedure with an
inappropriate parameter does not invoke the argument testing. The
following error message is not issued by p
. It is caused
by the attempt to evaluate 0 $ n
:
>> f := proc(n) begin p(n) end_proc: f(13/2)
Error: Illegal argument [_seqgen]; during evaluation of 'p'
We switch on the ``debugging mode'' of
testargs
:
>> testargs(TRUE):
Now also a non-interactive call to p
produces an informative error message:
>> f(13/2)
Error: expecting a positive integer [p]
We clean up, restoring the ``standard mode'' of
testargs
:
>> testargs(FALSE): delete f, g: