For example, I define
@multimethod
def f(x:list[str]):
return 1
@multimethod
def f(x:list[int]):
return 0
then
f([])
gives error
DispatchError: ('f: 2 methods found', (<class 'multimethod.<class 'list'>'>,), [(<class 'multimethod.list[str]'>,), (<class 'multimethod.list[int]'>,)])
how to define a case specific for empty list?
Given that you're checking some property of your list you should use
@overloadinstead of@multimethodthat way you can supply some custom predicate. Note as per the documentation "Overloads dispatch on annotated predicates. Each predicate is checked in the reverse order of registration" so be carefull with the order. Anyways we can define some simple functionisinstance(lst, list) and not lstand use lambda notation to write:And it all seems to work as expected. If you have issues with the
isinstanceyou can also usemultimethod's built inisafunction like this: