[PEAK] Re: ruledispatch

Phillip J. Eby pje at telecommunity.com
Mon Mar 13 13:46:37 EST 2006


At 07:30 PM 3/13/2006 +0100, Elvelind Grandin wrote:
>I'm just looking at ruledispatch for the first time and have a small question.
>I remeber reading somewhere that when there are more than one rule
>that applies it uses the most specific.
>What does most specifc means in that contex? I just gets an error when
>more than one rule applies.

If rule A applies whenever B applies, but B doesn't always apply when A 
applies, then B is more specific than A.  For example, 
isinstance(something, int) is more specific than 
isinstance(something,object), because something can be an object but not an 
int -- but an int is always an object.

Similarly, the expression "x>10" is more specific than "x>1", because if 
x>10, then it's >1.  But x>1 can be true without x>10 being true.

If neither condition implies the other, then the rule is ambiguous.  For 
example, x>1 and x<10 can apply at the same time.  Which one is more 
specific?  Neither.  You have to use "and" conditions to disambiguate this, 
e.g., by making explicit rules for "x>1 and x<10", "x<=1", and "x>=10".

In general, if you want one rule to override another, you should "and" its 
condition with the one you want to override.  For example if you have a 
rule with condition A, and you want it to override condition B, you should 
"and" it with condition B, e.g. when("A and B").

If you have multiple rules to override, you can "or" them, so if A is to 
override both B and C, you can use "A and (B or C)", as this is equivalent 
to "(A and B) or (A and C)".  If/when I ever get back to work on 
RuleDispatch I will probably make some kind of API to do this 
automatically, so you can just say "this method should override these other 
methods", and get the necessary conditions defined automatically.  But for 
now you need to do it by hand.




More information about the PEAK mailing list