Next: , Up: Tutorial   [Contents]


4.1 A simple greeter

First, let’s import Goblins:

> (use-modules (goblins))

(NOTE: See TEMPORARY Live hacking ``fix'' if you haven’t.)

Goblins actors live in special event loops called vats. Let’s 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 let us implement a friend who will greet us:

(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)))

Let’s 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 we will use to make our 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.

We 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, 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.

We 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. We can run also run code within a-vat like so:

scheme> (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, then the actors within the vats do their own thing.


Next: , Up: Tutorial   [Contents]