Next: Simple Mint, Previous: Sealers, Up: actor-lib: A standard library of sorts [Contents][Index]
The (goblins actor-lib selfish-spawn)
module provides a
spawn
-like procedure which spawns objects able to reference
themselves.
Construct and return a reference to a new object which has a reference to itself.
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.
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))) (methods ... ((top) (car ($ cards))) ((draw) (let ((top ($ self 'top))) ($ cards (cdr ($ cards))) top)) ...))
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>