For Development HEAD DRAFTSearch (procedure/syntax/module):

11.72 srfi.253 - Data type checking

Module: srfi.253

This SRFI defines forms to check the types of variables and arguments.

Macro: check-arg type-pred expr :optional caller

[SRFI-253]{srfi.253} Checks that expr conforms to type-pred. The type-pred argument is either a type predicate that returns true if the value of expr has the expected type, or a type object to be passed to of-type? for checking. (Note: Passing a type object is Gauche’s extension to the SRFI).

SRFI specifies the check is optional; an implementation may choose to omit the check for higher-optimization level, for example. Currently Gauche always perform the check, but we may add some options to omit it in future.

The optional caller argument (evaluated) can be used to indicate who is signalling the error.

NB: Gauche has provided undocumented check-arg macro for long time, to be used in early SRFIs. Since existing code may rely on it, we still provide it in the core (srfi.253 merely re-exports it), but the users should use srfi.253 explicitly if they want check-arg marco. Doing so is portable as well. Built-in support may be dropped in future.

The rest of the module consists of helper macro for common Scheme forms that call check-arg.

Macro: values-checked (type-pred …) val …

[SRFI-253]{srfi.253} Returns multiple values listed in val …, but each of val is checked with the corresponding type by (check-arg type val). In other words, you can add type checking to (values val …).

Macro: check-case expr (type-pred body …) … :optional (else else-body …)

[SRFI-253]{srfi.253} Evaluates expr, and it is checked against each type-pred in the same way as check-arg, in order. That is, type-pred must be a type predicate or a type object.

If the value of expr satisfies no type-pred, else-body … are evaluated if the else clause is provided, or an error is thrown (Gauche always throws an error in that case, but SRFI does not require it.)

See also typecase (see Conditionals).

Macro: lambda-checked formals+ body …
Macro: case-lambda-checked (formals+ body …) …

[SRFI-253]{srfi.253} Like lambda and case-lambda, but you can put (arg type-pred) in place of an argument in the formals, excep the “rest” argument. Such argument is checked with (check-arg arg type-pred 'lambda-checked).

(lambda-checked (obj (i integer?) (j integer?) . opts)
  ...)

≡
(lambda (obj i j . opts)
  (check-arg i integer? 'lambda-checked)
  (check-arg j integer? 'lambda-checked)
  ...)
Macro: define-checked (name . formals+) body …
Macro: define-checked name type-pred value

[SRFI-253]{srfi.253} The first form is a shorthand of (define name (lambda-checked formals+ body …, except that 'name is passed to the caller argument of check-arg.

The second form is a shorthand of (define name (values-checked (type-pred) value).

Macro: define-record-type-checked type-name (constructor arg …) predicate field …

[SRFI-253]{srfi.253} Defines a record type with checked constructor and field accessors/modifiers, like define-record-type.

Each field is the form either (field-name type-red accessor-name) or (field-name type-pred accessor-name modifier-name), where type-pred is used with values-checked and check-arg to ensure that accessor and modifier return checked data and check new data respectively. define-record-type-checked also guarantees that initial values passed to constructor are satisfying the predicates provided in field specification.



For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT