For Gauche 0.9.5

Next: , Previous: , Up: Core library   [Contents][Index]

6.1 Types and classes

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, through define-record-type. You can use record types in Gauche as well, via gauche.record module. 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 with brackets < and >, e.g. <string>. (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:

Builtin Class: <top>

This class represents the supertype of all the types in Gauche. That is, for any class X, (subtype? X <top>) is #t, and for any object x, (is-a? x <top>) is #t.

Builtin Class: <bottom>

This class represents the subtype of all the types in Gauche. For any class X, (subtype? <bottom> X) is #t, and for any object x, (is-a? x <bottom>) is #f.

There’s no instance of <bottom>.

Note: Although <bottom> is subtype of other types, the class precedence list (CPL) of <bottom> only contains <bottom> and <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 CPL of <bottom> every time a new class is defined or an existing class is redefined. Procedures subtype? and is-a? treat <bottom> specially.

One of use case of <bottom> is applicable? procedure. See Procedure class and applicability.

Builtin Class: <object>

This class represents a supertype of all user-defined classes.

Function: class-of obj

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).

Function: is-a? obj class

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, is-a? also triggers instance update. See Class redefinition for the details.

Function: subtype? sub super

Returns #t if a class sub is a subclass of a class super (includes the case that sub is super). Otherwise, returns #f.

(The name subtype? is taken from Common Lisp’s procedure subtypep.)

Next: , Previous: , Up: Core library   [Contents][Index]