#๐Ÿ”’ match case problem

30 messages ยท Page 1 of 1 (latest)

paper jackal
#

when i type to use a variable in match.. case (after the case keyword) it thinks it is a new variable created for the default case for example :

v = 123
match some_other_variable:
  case v:
    code(v)
  case 321:
    cde(321)

this here gives me this errors :
Irrefutable pattern is allowed only for the last case statement

and the code under the 2nd case is unreachable

sudden solarBOT
#

@paper jackal

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

paper jackal
#

is the problem that hard lol ?

merry flume
#

You can't use case like that, yeah. Consider just an if-else tree.

meager tendon
paper jackal
#

i rememeber i've heard of some keyword i can use in this case

#

it was smth like var.. vars global ?

#

so its smth like vars.v

#

i know vars is a dict thing

meager tendon
paper jackal
#

i know

#

ofc its not that code

#

the code i showed was just an example

meager tendon
#

Then could you show the actual code in question, or a more concrete example? Since in the given example I don't see why you would go for match or vars over if or just calling the function.

paper jackal
#
  def eval(self):
    pc = 0
    while pc < len(self.bytes):
      match self.bytes[pc]:
        case NOP:
          pc += 1
        
        case HALT:
          pc = len(self.bytes)
#

note :
NOP and HALT are imported from other file

merry flume
#

yeah, that's the sort of thing you'd just if-else for

#

python's match-case isn't like C's switch-case, it's more like pattern matching in languages like Rust.

paper jackal
#

hmm are you sure smth like enums are not gonna help ?

meager tendon
#

It's called Structural Pattern Matching for a reason. Enums can help, and you can in fact do the same "check you have all enum variants covered" in both a match and an if-elif-else chain, where where the else is the same as the default case, containing assert_never

paper jackal
#

there are like 20 other vaiables just like NOP writing self.bytes[pc] == NOP evrey time will be SOO long

merry flume
#

I mean, you could get it to work, sure - if your case looks like case thing.attr, then it's not a name capture. which means making an IntEnum would work.

meager tendon
#

You can always copy and paste just the elif self.bytes[pc] == part a bunch of times, so it's the same amount of writing as using a match

paper jackal
meager tendon
#
    def eval(self):
        pc = 0
        while pc < len(self.bytes):
-           match self.bytes[pc]:
-               case NOP:
-                   pc += 1
+           if self.bytes[pc] == NOP:
+               pc += 1
        
-               case HALT:
-                   pc = len(self.bytes)
+           elif self.bytes[pc] == HALT:
+               pc = len(self.bytes)

Sure the elifs are longer, but I prefer having the contents have one less level of indentation. In the end I suppose it's more down to style choice (if your opcodes are actually thing.opcode where match will work)

#

Looking into it more, I don't think there is a way to get it to work if you just have the variables in scope, at least without some unholy ast/exec nonsense. They'll need to be part of some other thing so you can do thing.NOP.

paper jackal
#

!close

sudden solarBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.