What about a `__match__()` magic function:
so

given object:
     case Point(x,y,3):print(x,y)
     case Point(1,2,3):something_magic()
     case x,y,Point(a,b,c),1:this_is_my_tuple()


be same than writing

try:
   x,y=Point.__match__(object,("x","y",None),(None,None,3))
except MatchError:
    try:
         Point.__match__(object,(None,None,None),(1,2,3))
    except MatchError:
         try:
              x,y,var1,var2=object

              #literal testing
              if var2 != 1:
                    raise MatchError()

              a,b,c=Point.__match__(var1,("a","b","c"),(None,None,None))
              #var1 and var2 would not be really created, they are placeholders
         except MatchError:
              #nodefault
              raise
          else:
              this_is_my_tuple
    else:
         something_magic()
else:
   print(x,y)

and let the __match__ functions know what to do to check if arguments are correct

I purposely did let drop the dictionary and keyword arguments cases, finding them too
much hard to understand

2016-05-30 20:35 GMT+02:00 Steven D'Aprano <steve@pearwood.info>:
On Sat, May 28, 2016 at 02:13:33PM +1200, Greg Ewing wrote:
[...]
> Finally we could allow "case class" to be abbreviated to
> just "class":
>
>    switch obj:
>       class Intersection(
>          class Line(class Point(?x1, ?y1), class Point(?x2, ?y2)),
>          class Line(class Point(?x3, ?y3), class Point(?x4, ?y4))):
>
> Is that unacceptably verbose? I don't know.

Verbose, not so much. Cryptic? Hell yes! Who is going to be able to
guess what it means? We're no longer even in the same galaxy as
executable pseudo-code.

Try this as an exercise: given the above, explain in plain English what
it does and what the result will be. What exactly is being matched?

Those familiar with C-like switches are going to be totally confused.
They'll probably wonder if you are matching "if obj == the class
Intersection", and have no idea what's going on with the nested Line and
Point stuff.

Those familiar with classes and the class keyword are going to wonder
what class definitions are doing inside the class declaration. Does this
mean we can now do this?

class Child(class Parent: pass):
    def method(self): ...

Obviously not, but that's what it looks like.

I wonder whether the real problem here is that pattern matching as a
concept is simply too concise for mere mortals? Once you get past the
trivial Haskell-esque:

def factorial(n):
    switch n:
        0: return 1
        n: return n*fact(n-1)

its just doing too much in too little code to be easily comprehensible.



--
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/