Next: Objects which contain objects, Previous: State as updating behavior, Up: Tutorial [Contents][Index]
The most common way to select different sub-behaviors isn’t by the
number of arguments. It’s much more common to have “methods” that
are selected by name. You can use the methods
macro to do just
that:
(use-modules (goblins) (goblins actor-lib methods)) (define* (^mcell bcom #:optional [val #f]) (methods ((get) val) ((set new-val) (bcom (^mcell bcom new-val)))))
The use of ^mcell
is similar, but now you pass in a first
argument as a symbol selecting the method, and the rest of the
arguments are passed to the object:
goblins[1]> (define jar (spawn ^mcell "cookies")) goblins[1]> ($ jar 'get) ; => "cookies" goblins[1]> ($ jar 'set "crumbs") goblins[1]> ($ jar 'get) ; => "crumbs"
Methods aren’t particularly magical… the methods
macro
just returns a procedure which is convenient to use for procedure
dispatch:
> (define foo-or-bar (methods ((foo) 'called-foo) ((bar arg) (list 'called-bar-with arg)))) > (foo-or-bar 'foo) ; => called-foo > (foo-or-bar 'bar "meep") ; => (called-bar-with "meep")
So really, methods
just makes a particularly useful kind of
behavior procedure for you, one which takes a first argument for the
method name, grabs the rest of the arguments, and then applies them to
the relevant method.