Next: State as updating behavior, Up: Tutorial [Contents][Index]
First, import Goblins:
> (use-modules (goblins))
(NOTE: See TEMPORARY: Live hacking “fix” if you haven’t.)
Goblins actors live in special event loops called vats. Make a vat to play around with and “enter” it at the REPL:
scheme> (define a-vat (spawn-vat)) scheme> ,enter-vat a-vat goblins[1]>
Now implement a friend who will greet you:
(define (^greeter bcom our-name) ; constructor (outer procedure) (lambda (your-name) ; behavior (inner procedure) (format #f "Hello ~a, my name is ~a!" your-name our-name)))
Give it a try by instantiating an instance of ^greeter
using spawn
and assign her to the variable alice
:
> (define alice (spawn ^greeter "Alice"))
The first argument to spawn
is the constructor used to
make the Goblins object, in this case ^greeter
. The remaining
arguments will be passed to the constructor, along with an implicit
argument which can be used to change object behavior called
bcom
(pronounced “become”). Thus "Alice"
maps to
our-name
.
You can invoke alice
by using $
for a synchronous
call-return operation:
goblins[1]> (define alice (spawn ^greeter "Alice")) goblins[1]> ($ alice "Alfred") ; => "Hello Alfred, my name is Alice!"
The first argument to $
is the object to be invoked, and the
remaining arguments are passed to the “behavior” of the object.
Thus your-name
in ^greeter
’s behavior procedure is bound
to "Alfred"
. The behavior procedure thus proceeds with
formatting and returning the greeting string.
You can exit the subrepl for a-vat
with ,q
(the same way
as if Guile put us in a subrepl to debug an error):
goblins[1]> ,q scheme>
As you have probably inferred, Goblins-specific operations such as
$
and spawn
happen within a context (typically a
vat, though there is an even lower-level structure called an
actormap which we will see more about later). However, the
subrepl entered by ,enter-vat
is a convenience for hacking. You
can run also run code within a-vat
like so:
scheme> (call-with-vat a-vat (lambda () ($ alice "Arthur"))) ; => "Hello Arthur, my name is Alice!"
Very few operations in Goblins spend time operating on vats directly; typically commands like the above only happen when bootstrapping the initial set of actors within a vat or when hacking, after which the actors within the vats do their own thing.
Next: State as updating behavior, Up: Tutorial [Contents][Index]