1 Introduction
2 Tour
3 Basic Interface
object?
object-parent
object-set!
object-get
object-get
object-apply
object-apply/ noslot-thunk
object-raw-clone/ no-slots-copy
object-raw-clone/ copy-immed-slots
object-raw-clone/ copy-all-slots
current-root-object
4 Terse Syntax
^
!
!
?
@
@
%
5 History
6 Legal
Version: 0.4

Protobj: Prototype-Delegation Object Model in Scheme

Neil Van Dyke

License: LGPL 3   Web: http://www.neilvandyke.org/protobj/

 (require (planet neil/protobj:1:2))

1 Introduction

Protobj is a Scheme library that implements a simple prototype-delegation object model, somewhat similar to that of Self, and also related to those of SLIB object and OScheme. Protobj was written mainly as a syntax-rules learning exercise, but also because people ask about prototype object models for Scheme from time to time. Like most object systems, Protobj should be regarded as an amusement. The Protobj library defines both a verbose set of procedures, and terse special syntax.

Protobj is based on objects with named slots that can contain arbitrary values. Object have immediate slots, and single parent objects from which additional slots are inherited. When setting in a child object a slot inherited from the parent, a new immediate slot is created in the child so that the parent is unaffected and the slot is no longer inherited.

Methods are simply closures stored in slots. When a method is applied, the first term of the closure is the receiver object. Unlike Self, getting the contents of the slot is distinguished from invoking a method contained in the slot. This distinction was made due to the way first-class closures are often used in Scheme.

An object is cloned by invoking the clone method. The default root object’s clone method creates a new child object without any immediate slots, rather than copying any slots. This behavior can be overridden to always copy certain slots, to copy immediate slots, or to copy all inherited slots. An overriding clone method can be implemented to apply its parent’s clone method to itself and then set certain slots in the new child appropriately.

Protobj requires R5RS, SRFI-9, SRFI-23, and SRFI-39.

2 Tour

The following is a quick tour of Protobj using the terse special syntax.

3 Basic Interface

The basic interface of Protobj is a set of procedures.

(object? x)  any/c
  x : any/c

Predicate for whether or not x is a Protobj object.

(object-parent obj)  any/c
  obj : any/c

Yields the parent object of object obj.

(object-set! obj slot-symbol val)  any/c
  obj : any/c
  slot-symbol : any/c
  val : any/c

Sets the slot identified by symbol slot-symbol in object obj to value val.

(object-get obj slot-symbol)  any/c
  obj : any/c
  slot-symbol : any/c

Yields the value of slot named by symbol slot-symbol in object obj (immediate or inherited). If no slot of that name exists, an error is signaled.

(object-get obj slot-symbol noslot-thunk)  any/c
  obj : any/c
  slot-symbol : any/c
  noslot-thunk : any/c

Yields the value of slot named by symbol slot-symbol in object obj (immediate or inherited), if any such slot exists. If no slot of that name exists, then yields the value of applying closure noslot-thunk.

(object-apply obj slot-symbol arg ...)  any/c
  obj : any/c
  slot-symbol : any/c
  arg : any/c
  ... : any/c

Applies the method (closure) in the slot named by slot-symbol of object obj. The first term of the method is obj, and one or more arg are the remaining terms. If no such slot exists, an error is signaled.

(object-apply/noslot-thunk obj    
  noslot-thunk    
  slot-symbol    
  arg    
  ...)  any/c
  obj : any/c
  noslot-thunk : any/c
  slot-symbol : any/c
  arg : any/c
  ... : any/c

Like object-apply, except that, if the slot does not exist, instead of signalling an error, the value is the result of applying noslot-thunk.

(object-raw-clone/no-slots-copy obj)  any/c
  obj : any/c
(object-raw-clone/copy-immed-slots obj)  any/c
  obj : any/c
(object-raw-clone/copy-all-slots obj)  any/c
  obj : any/c

These procedures implement different ways of cloning an object, and are generally bound as clone methods in root objects. /no-slots-copy does not copy any slots, /copy-immed-slots copes immediate slots, and /copy-all-slots copies all slots including inherited ones.

(current-root-object)  any/c
(current-root-object x)  void?
  x : any/c

Parameter for the default root object. The initial value is a root object that has object-raw-clone/no-slots-copy in its clone slot.

4 Terse Syntax

Since Protobj’s raison d’etre was to play with syntax, here it is. Note that slot names are never quoted.

(^ obj)

Parent of obj.

(! obj slot val)
(! obj ( slot val)  ...)

Sets object obj’s slot slot’s value to val. In the second form of this syntax, multiple slots of obj may be set at once, and are set in the order given.

(? obj slot ...)

Yields the values of the given slots of obj. If more than one slot is given, a multiple-value return is used.

(@ obj slot arg ...)
(@ obj ( slot arg ...)  ...)

Applies obj’s slot method, with obj as the first term and args as the remaining terms. In the second form of this syntax, multiple methods may be applied, and the value is the value of the last method application.

(% obj ( slot val)  ...)

Clones object obj, binding any given slots to respective given vals.

5 History

6 Legal

Copyright (c) 2005–2011 Neil Van Dyke. This program is Free Software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License (LGPL 3), or (at your option) any later version. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See http://www.gnu.org/licenses/ for details. For other licenses and consulting, please contact the author.

Standard Documentation Format Note: The API signatures in this documentation are likely incorrect in some regards, such as indicating type any/c for things that are not, and not indicating when arguments are optional. This is due to a transitioning from the Texinfo documentation format to Scribble, which the author intends to finish someday.