Addition of addition to match case

current scenario -

x = 'cd'
match x:
  case ('c' + 'd') | 'e':
    print(1)
  case 'a':
    print(2)

gives,

    case ('c' + 'd') | 'e':
              ^
SyntaxError: invalid syntax

expected scenario -

  1. addition/concatenation works in match case, just like it does outside of match case and the above outputs 1

What is so special about addition only that you want to support addition, and not every other unlimited expression?

Have you read the PEPs on pattern matching to see why expressions aren’t supported?

yes it turns out *, - none of it work.

yes I have read the PEPs, but mostly for code that works.

where have they mentioned the reasoning for it, I could not find it, as there are four PEPs for match case and it is a bit difficult to find in them.

What would be the semantics?

match foo:
    case bar: # matches everything
        ...

match foo:
    case (bar, baz): # matches 2-tuples
        ...

match foo:
    case bar + baz: # what does this match?
         ...

probably ambiguous types should not be valid, but,
for concatenation,

match x:
  case str('x') + str(): # matches any string that begins with 'x'
match x:
  case list('a') + list(): # matches any list, that has first element as 'a'

could be a possibility

How do you tell if a type is ambiguous or not?

For what it’s worth, the first one can be spelt as x.startswith('x'), and I don’t have use cases in mind where the sequence of characters in a string lends itself well to pattern matching rather than such string processing methods. The second one can already be spelt as

match x:
    case ('a', *other):
        ...

(I was imprecise in my other reply: (foo, bar) matches sequences, not just tuples.)

on further investigation, I found out that there is some issue with tuple vs list matching using the technique you described.

x = ['a', 'b', 'c', 'd']
y = ('a', 'b', 'c')
match y:
  case ['a', *others]:
    print(1)
  case ('a', *others):
    print(2)

gives 1, which should give 2
one way to make it work is this,

x = ['a', 'b', 'c', 'd']
y = ('a', 'b', 'c')
match y:
  case list(['a', *others]):
    print(1)
  case tuple(('a', *others)):
    print(2)

which gives 2

I am still investigating, use cases for these operators to work in match case, as it is not only +, but even other operators, like *, -.
but the main reason is to make the operators work the same way as they do outside of the match case statement for these types.

for example, if one wants to do a exact list match for,

x = [1, 2] * 10000
match x:
  case list([1, 2, 1, 2, ...]): # in the current scenario, would have to write it 10000 times, which is not feasible

better would have been,

x = [1, 2] * 10000
match x:
  case list([1, 2] * 10000):

but this syntax is invalid currently.

Square bracket and parenthesis both match sequences, just like it is mentioned in PEP 636

You’re probably misunderstanding what case does, it is pattern match, not a dynamic comparison of values, you should use simple if else statements for that, or you can use case ... if ...:

x = [1, 2] * 10000
match x:
  case list() if x == [1, 2] * 10000:
    print("matches")

upon further investigation I found one more way to achieve this, that is with the use of class attributes,

class A:
  lst = [1, 2] * 100
x = [1, 2] * 100

match x:
  case A.lst:
    print(1)

I personally prefer the class attribute way over using if in the match case.

Using case str('x') + str() for string prefixes is an interesting
idea, but that can already be done using guard clauses:

match value:
    case str() as s if s.startswith(prefix):
        ...