On this page:
require
define-class
new
send
define
this
fields
5.92

2 Class 0

 #lang class/0 package: base

The class/0 language is the most basic version of the class system. In essence, it extends Intermediate Student with Lambda with the ability to define classes, construct objects that are instances of classes, and to send such objects messages to call methods.

syntax

(require module-name ...)

Imports all the modules named module-names.

syntax

(define-class class-name
   field-names
   method-or-test ...)
 
field-names = 
  | (fields field-name ...)
     
method-or-test = method
  | test
     
method = (define (method-name variable ...) body)
Defines a new class named class-name with fields field-names and methods method-names. The class has one additional method for each field name field-name, which access the field values.

A method is defined with (define (method-name variable ...) body). Such a definition extends the set of messages every instance of class-name understands. When an object is sent a method-name message and some values, the body of the method definition is evaluated with the values of the arguments in place of the variables. Within a method, this refers to the current object, i.e. the object whose method was called. Every class implicitly defines an accessor methods for each field, so (send this field-name) refers to the value of the field named field-name of this.

A test can be any form of check-expect. Conceptually, each test is lifted out of the class definition and does not exist inside any instance of class-name, therefore tests cannot reference this.

syntax

(new class-name expression ...)

Constructs an object that is an instance of class-name and whose fields are the values of each expression. The number of expressions must be equal to the number of fields in class-name’s definition.

syntax

(send object method-name expression ...)

Sends object the message method-name with the values of the expressions as arguments, that is, call object’s method named method-name with the given arguments.

syntax

(define (method-name variable ...) body)

Within a class definition, this form defines a method. When a method is invoked, occurrences of this within body are bound to the receiver object.

Outside of a class definition, this form defines a function; it is equivalent to define in ISL/λ.

syntax

this

See define-class. This form is not allowed outside of a method definition.

syntax

(fields id ...)

See define-class. This form is not allowed outside of a class definition.