Previous: How Guile does it, Up: Data Representation


18.3 Defining New Types (Smobs)

Smobs are Guile's mechanism for adding new non-immediate types to the system.1 To define a new smob type, the programmer provides Guile with some essential information about the type — how to print it, how to garbage collect it, and so on — and Guile returns a fresh type tag for use in the first word of new cells. The programmer can then use scm_make_gsubr to make a set of C functions that create and operate on these objects visible to Scheme code.

(You can find a complete version of the example code used in this section in the Guile distribution, in doc/example-smob. That directory includes a makefile and a suitable main function, so you can build a complete interactive Guile shell, extended with the datatypes described here.)


Footnotes

[1] The term “smob” was coined by Aubrey Jaffer, who says it comes from “small object”, referring to the fact that only the cdr and part of the car of a smob's cell are available for use.