You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to define multiple dispatching for a function f(x, y) that I know should always be commutative (f(x, y) == f(y, x)). Does multipledispatch support this? Basically, I only want to define f(type_A, type_B) and have it automatically define f(type_B, type_A).
Activity
mrocklin commentedon Jun 17, 2020
The dumb way to do this is the following:
There may also be another way to do it (I'm no longer up to date on this library)
asmeurer commentedon Jun 17, 2020
Looking at the code, I don't think this is implemented. But I wonder if it can be done simply by subclassing Dispatcher like
and then also redefine
dispatchto useCommutativeDispatcher.Does that work? I thought tuple meant union. So wouldn't that also define
f(A, A)andf(B, B)?mrocklin commentedon Jun 17, 2020
Yes, you're correct. So maybe you want ...
As you say, making a wrapper would not be hard.
asmeurer commentedon Jun 17, 2020
Yes, that's what I want. Except I don't want to type it twice for each type, because I know that
fis always commutative.Actually, maybe it will be better make a wrapper at the dispatcher level, rather than Dispatcher.
(I haven't tested this)
(functions that return functions don't compose so easily in Python. I think we need monads or something ;-)