[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [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.

In this section we introduce the most basic interface to the Gauche’s type system. See section 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.

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 exsting user-defined classes. If the new definition has different configuration of the instance, class-of on existing instance triggers instance updates; See section Class redefinition for the details. Using current-class-of supresses instance updates (See section 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.)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on May 28, 2012 using texi2html 1.82.