Next: , Previous: , Up: actor-lib: A standard library of sorts   [Contents][Index]

6.14 Selfish-Spawn

The (goblins actor-lib selfish-spawn) module provides a spawn-like procedure which spawns objects able to reference themselves.

Procedure: selfish-spawn constructor args …

Construct and return a reference to a new object which has a reference to itself.

  • constructor: Procedure which returns a procedure representing the object’s initial behavior; implicitly passed a first argument, bcom, which can be used to change the object’s behavior, and a second argument, self, which is a reference to the object being constructed.
  • args: Remaining arguments passed to constructor.

This procedure is useful for patterns requiring object introspection. However, it is usually possible to achieve the same results without selfish-spawn so it should be used sparingly.

Here is a simplified example of a deck of playing cards using its own method within another method:

(define* (^deck bcom self #:optional crds)
  ;; define helpers etc. here...

  (define-cell cards (or crds (init-deck)))
   ((top) (car ($ cards)))
    (let ((top ($ self 'top)))
      ($ cards (cdr ($ cards)))

This object can then be used more or less like any other:

> (use-modules (goblins)
               (goblins actor-lib selfish-spawn))
;; vat setup and entry
> (define deck (selfish-spawn ^deck))
> ($ deck 'draw)
=> #<some-card>