These are chat archives for evhub/coconut

23rd
Jun 2018
yggdr
@yggdr
Jun 23 2018 21:13

I have a problem here with pattern matching functions that manifests itself like to:

def func():
    print('starting func')
    other_match_func()
    print('stopping func')

@addpattern(func)
def func():
    print('starting second func')
    print('stopping second func')

match def other_match_func(x):
    print('starting other func')
    print('stopping other func')

func()

gives:

starting func
starting second func
stopping second func

So the problem is twofold:
Both definitions of func could in principle match the given pattern in the call. The real code I experience this in is a bit more complex and I'd like to rely on the fact that only exactly one "instance" of func ever gets called, namely the first one matching in definition order.
In the first matching func definition I call another pattern matching function, which doesn't have a match. Instead of receiving any kind of error, the MatchError raised during the call to other_match_funcis catched within the add_pattern_func of the addpattern-decorator of func, just as if this instance of func hadn't matched. That results in the decorator trying to match more func versions, and the next one that happens to also match is then being called. This can give the programmer a real headache trying to figure out why the hell in the middle of the properly called first func, suddenly the second version gets called.

Might I suggest to use a personalised MatchError class within the addpattern decorator, so that only the specific class for this function is catched, and others are passed through? If that sounds ok, I'd like to try my hand on implementing this.