>
>
>     class MatchType(type):
>         def __eq__(self, other):
>             return self == type(other)
>
> Or I even want to match:
>
>     type[int], y < 42, z = vec
>
> Well, I can’t think of a language where you can actually match that way.
> That doesn’t necessarily mean we should disallow it, but I can’t imagine
> how the compiler and interpreter are supposed to figure this out.
>

Actually, you've given the solution already:

class MatchLT:
    def __eq__(self, other):
        return self < other

MatchType(int), MatchLT(42), z = vec


>     MatchType(int), MatchFunc(lambda y: y<42) as y, z = vec
>

Or yeah... to be generic about predicate, that's it.

The very ancient library PEAK in Python did multiple dispatch on a
predicative basis, which amounts to pattern matching. For example, from a
very old article I wrote on this:

import dispatch
@dispatch.generic()
def doIt(foo, other):
    "Base generic function of 'doIt()'"
@doIt.when("isinstance(foo,int) and isinstance(other,str)")
def doIt(foo, other):
    print "foo is an unrestricted int |", foo, other
@doIt.when("isinstance(foo,str) and isinstance(other,int)")
def doIt(foo, other):
    print "foo is str, other an int |", foo, other
@doIt.when("isinstance(foo,int) and 3<=foo<=17 and isinstance(other,str)")
def doIt(foo, other):
    print "foo is between 3 and 17 |", foo, other
@doIt.when("isinstance(foo,int) and 0<=foo<=1000 and isinstance(other,str)")
def doIt(foo, other):
    print "foo is between 0 and 1000 |", foo, other

doIt( 1, 'this')  # -> foo is between 0 and 1000 | 1 this
doIt('x', 1234)   # -> foo is str, other an int | x 1234
doIt(10, 'this')  # -> foo is between 3 and 17 | 10 this
doIt(20, 'this')  # -> foo is between 0 and 1000 | 20 this
doIt(-7, 'this')  # -> foo is an unrestricted int | -7 this
try: doIt(2222, 66)
except dispatch.interfaces.NoApplicableMethods:
    print "No Applicable Methods" # -> No Applicable Methods

(http://gnosis.cx/publish/programming/charming_python_b22.html)

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HUO7MLATR2A32PUJG2KDJ3V44DO662OV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to