Type Extensions

next up previous
Next: Environment Representation Up: libscheme Architecture Previous: Primitive Syntax

Type Extensions

Scheme as defined in its standard has the following data types: boolean, list, symbol, number, character, string, vector, and procedure. While Scheme in its current form does not allow the creation of user-defined types, the libscheme system allows users to extend the type system with new types by calling the scheme_make_type() function with a string representing the name of the new type. This function returns a type object that can be used as a type tag in subsequently created objects. Normally, types are created in a file's initialization function and objects of the new type are created using a user-defined constructor function that allocates and initializes instances of the type.

In figure 8 we see the constructor for the dw_debug_type type from our dwarfscheme example. It accepts an object of type Dwarf_Debug, a pointer to a C structure defined in the libdwarf library, allocates a new Scheme_Object, sets the object type, and stores the pointer to the foreign structure into the ptr_val slot of the object.

Figure 8: An object constructor

It is often convenient to define a macro that checks whether a libscheme object is of a specified type. The macro defined in dwarfscheme for the DWARF debug object looks like this:

#define DW_DEBUGP(obj) (SCHEME_TYPE(obj) == dwarf_debug_type)
The `P' at the end of DW_DEBUGP indicates that the macro is a predicate that returns a true or false value. All of the builtin types have type predicate macros of this form (e.g., SCHEME_PAIRP, SCHEME_VECTORP, etc.).

Brent Benson
Mon Sep 19 16:03:14 EDT 1994