Scheme is a dynamically and strongly typed language. That is, every value knows its type at run-time, and the type determines what kind of operations can be applied on the value.
In Gauche, classes are used to describe types. A class is also an object you can handle at runtime. You can also create a new class in order to have objects with distinct types.
Since R6RS, Scheme got a standard way to define a new type,
define-record-type. You can use record types
in Gauche as well, via
See Record types. Internally a record type is implemented
as a class.
In this section we introduce the most basic interface to the Gauche’s type system. See Object system, for the details of how to define your own classes and creates values (instances).
Predefined classes are bound to a global variable; Gauche’s
convention is to name the variable that holds a class
(It’s nothing syntactically special with these brackets; they’re valid
characters to consist of variable names).
We’ll introduce classes for each built-in type as we go through
this chapter. Here are a few basic classes to start with:
This class represents the supertype of all the types in Gauche.
That is, for any class
(subtype? X <top>) is
and for any object
(is-a? x <top>) is
This class represents the subtype of all the types in Gauche.
For any class
(subtype? <bottom> X) is
and for any object
(is-a? x <bottom>) is
There’s no instance of
<bottom> is subtype of other types,
the class precedence list (CPL) of
<bottom> only contains
<top>. It’s because it isn’t
always possible to calculate a linear list of all the types.
Even if it is possible, it would be expensive to check and update the
<bottom> every time a new class is defined or
an existing class is redefined. Procedures
One of use case of
See Procedure class and applicability.
This class represents a supertype of all user-defined classes.
Returns a class metaobject of obj.
(class-of 3) ⇒ #<class <integer>> (class-of "foo") ⇒ #<class <string>> (class-of <integer>) ⇒ #<class <class>>
Note: In Gauche, you can redefine existing user-defined classes.
If the new definition has different configuration of the instance,
class-of on existing instance triggers instance updates;
see Class redefinition for the details. Using
current-class-of suppresses instance updates
(see Accessing instance).
Returns true if obj is an instance of class or an instance of descendants of class.
(is-a? 3 <integer>) ⇒ #t (is-a? 3 <real>) ⇒ #t (is-a? 5+3i <real>) ⇒ #f (is-a? :foo <symbol>) ⇒ #f
Note: If obj’s class has been redefined,
triggers instance update.
See Class redefinition for the details.
#t if a class sub is a subclass of a class super
(includes the case that sub is super).
subtype? is taken from Common Lisp’s procedure