Sv: Re: [PEAK] Interfaces

Phillip J. Eby pje at telecommunity.com
Wed Jul 5 19:35:25 EDT 2006


At 11:23 PM 7/5/2006 +0200, gabriel.j at telia.com wrote:
>I am a bit confused as to how it works when you have several things
>that you wish to do with say a verb.
>Our current system (unfinished) uses a chain of reaction where each
>component of an object has to say that it doesn't object(no pun
>intended) to the action being performed before they actually do what
>they were designed to do. This is done with a simple try statement that
>takes care of errors and execution. Any object can abort the chain.
>
>What I need to know about the visitor pattern in your blog Phillip is
>what it handles.
>
>Say we want to just add a sentence if an action is performed.
>
>@drink.when('target isDrinkable()')
>drink(actor, target):
>     print 'glug glug'
>
>@drink.when('target isDrinkable()')
>drink(actor, target):
>     print 'ahhh'
>
>Will this print:
>glug glug
>ahhh
>
>or something entirely different?

Unless you use a custom method combination, it'll give you an error message 
about ambiguous rules.  You probably want to learn about "before", "after", 
and "around" methods, which work similarly to their counterparts in Lisp:

http://peak.telecommunity.com/DevCenter/CombiningResults

(If you did the above using "before" methods, I believe it would print 
"glug glug" "ahhh".)


>Is it possible to overload some kind of default action for an object?
>
>@drink.default('target isDrinkable()')
>drink(actor, target):
>     print 'glug glug'
>
>@drink.default('target isDrinkable()') -- this should be the new
>default
>drink(actor, target):
>     print 'you drink the liquid.'

Replace 'default' in the first case with 'when', and in the second case 
with 'around', and this will work for replacement.  Or, you could create a 
custom method combination using priorities, if you like.


>Can generic functions be methods?

Yes.


>Do methods have their own GF name
>spaces?  Or is it truly generic?

I'm not sure I know how to answer that question.  RuleDispatch and 
PEAK-Rules work on function *objects*, not function *names*.


>Please exuse my total lack of understanding of what is what I haven't
>been able to install peak it is quite frustrating.
>Is Peak dependent on a specific gcc-package to install with distutils?

Not as far as I know.  If you are having trouble, you can always install 
the packages (PyProtocols and RuleDispatch) without C acceleration, by 
checking them out of Subversion, and running "setup.py --without-speedups 
install" in each one.

PEAK-Rules on the other hand doesn't have any C code, as it's pure Python, 
but it's also still very new, doesn't support arbitrary test expressions, 
and has even less documentation than RuleDispatch.  On the other hand, its 
main module is only 650 lines long and so the code is probably a lot easier 
to understand than that of RuleDispatch.  :)




More information about the PEAK mailing list