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


6.2 Methods

The (goblins actor-lib methods) module provides a simple, single-dispatch method system for actor behaviors.

Here’s a constructor for an actor that can say hello or goodbye:

> (define (^hello-goodbye _bcom)
    (methods
     ((say-hello name)
      (format #f "Hello, ~a!" name))
     ((say-goodbye name)
      (format #f "Goodbye, ~a!" name))))

The methods macro is used to describe a collection of messages that an actor accepts. The first argument of the message is used to dispatch the proper behavior for handling that message.

Let’s try it out:

> (define hello-goodbye (spawn ^hello-goodbye))
> ($ hello-goodbye 'say-hello "Alice")
=> "Hello, Alice!"
> ($ hello-goodbye 'say-goodbye "Alice")
=> "Goodbye, Alice!"