weissjeffm / multimethods

Multimethods for Python, inspired by Clojure
Other
16 stars 3 forks source link

Cannot match on the value 0 #4

Closed jsofra closed 10 years ago

jsofra commented 10 years ago

I am unable to define a method that dispatches on the value 0.

from multimethods import MultiMethod

def dispatch(x): return x

foo = MultiMethod('foo', dispatch)

foo.addmethod(lambda x: "yep", 0)

multimethods.DispatchException: No matching method on multimethod 'foo' for '0', and no default method defined

weissjeffm commented 10 years ago

Nice find, I was testing the dispatch value for logical true, when I should have done "is not None". 0 is "falsey". Will fix and publish today. thanks!

weissjeffm commented 10 years ago

The fix stops caring about what the boolean interpretation of the dispatch value is.

As it looks for matches, it now starts off with Default, and if that ends up being the best match, it's returned (or throw exception if there's no default dispatch function). So now dispatch values of 0, False, and None should work fine.

weissjeffm commented 10 years ago

Fix is in 0.4.1