Next: Nonce Registry, Previous: Let-On, Up: actor-lib: A standard library of sorts [Contents][Index]
The (goblins actor-lib methods)
module provides a simple,
single-dispatch method system for actor behaviors.
Syntax to describe a collection of methods that an actor accepts. name is the symbol upon which the method dispatches, args are the arguments it accepts, and expr is the expression evaluated when the method is called.
As an example, consider this actor capable of saying hello or goodbye:
> (define (^hello-goodbye _bcom) (methods ((say-hello name) (format #f "Hello, ~a!" name)) ((say-goodbye name) (format #f "Goodbye, ~a!" name)))) > (define hello-goodbye (spawn ^hello-goodbye)) > ($ hello-goodbye 'say-hello "Alice") => "Hello, Alice!" > ($ hello-goodbye 'say-goodbye "Alice") => "Goodbye, Alice!"
Syntax to expand a collection of methods or an actor extends.
name, args, and expr are as in methods
.
So this would allow extending the previous example with a method to ask how the caller is:
> (define hello-goodbye-how-are-you (extend-methods hello-goodbye ((how-are-you name) (format #f "How are you, ~a?" name)))) > (hello-goodbye-how-are-you 'how-are-you "Alice") => "How are you, Alice?"
You may notice that when extending a live reference, the result is not
a reference to an actor, but rather a procedure which invokes the
original actor. extend-methods
can also be used to extend
methods directly, as in:
> (define behs (methods ((say-hello) (format #f "Hello!")) ((say-goodbye) (format #f "Goodbye!")))) > (define new-behs (extend-methods behs ((how-are-you) (format #f "How are you?")))) > (define (^hello-goodbye-how-are-you bcom) new-behs) > (define hello-goodbye-how-are-you (spawn ^hellow-goodbye-how-are-you)) > ($ hello-goodbye-how-are-you 'how-are-you) => "How are you?"
Next: Nonce Registry, Previous: Let-On, Up: actor-lib: A standard library of sorts [Contents][Index]