🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.python
2 messages
2 total messages Started by Lawrence D’Oliveiro Sun, 11 Oct 2026 21:26
Re: Taming The Switch-Statement
#1
Author: Lawrence D’Oliveiro
Date: Sun, 11 Oct 2026 21:26
82 lines
4167 bytes
On Sat, 10 Oct 2026 00:25:54 -0000 (UTC), I wrote:

> For comparison, here is an example of Python’s match-statement. Like
> all the good (i.e. non-C-based) designs, the designers saw no point in
> having fall-through.
>
> This is a very simple example, involving straight matching of values
> against instances of an enum. There are also more complex
> pattern-matching options available.

Here’s an example that matches the type of the match-expression (not
its value) against various event subclasses:

    events = iter(self.ws.events())
    while True :
        event = next(events, None)
        if event == None :
            break
        match event :
            case wsevents.AcceptConnection() :
                self.logger.debug("connection accepted")
            #end case

            case wsevents.RejectConnection() :
                raise RuntimeError \
                  (
                    "WebSockets connection rejected: code %d" % event.status_code
                  )
            #end case

            case wsevents.CloseConnection() :
                self.logger.debug("connection closing")
                if not self.closing :
                    self.closing = True
                    if ASYNC :
                        await self.sock.sendall(self.sock, self.ws.send(event.response()))
                    else :
                        self.sock.sendall(self.sock, self.ws.send(event.response()))
                    #end if
                #end if
            #end case

            case wsevents.Ping() :
                if ASYNC :
                    await self.sock.sendall(self.ws.send(event.response()))
                else :
                    self.sock.sendall(self.ws.send(event.response()))
                #end if
            #end case

            case wsevents.TextMessage() :
                self.partial += event.data
                if not event.message_finished :
                    break
                if self.partial != "" :
                    result = json.loads(self.partial)
                    self.partial = ""
                else :
                    result = None
                #end if
                yield result
            #end case

            case _ :
                raise RuntimeError \
                  (
                        "unexpected WebSocket event %s -- %s"
                    %
                        (type(event).__name__, repr(event))
                  )
            #end case
        #end match
    #end while

It is permissible to mix all the different kinds of matches in
different case-clauses within the same match-statement.

I’m still getting used to the match-statement, and I’m currently
thinking of dropping the “#end case” line, since each case-clause is
implicitly terminated by the following one (or the end of the
match-statement) anyway. The “#end match” will stay, of course.
Re: Taming The Switch-Statement
#3
Author: Jon Ribbens
Date: Sun, 11 Oct 2026 22:12
17 lines
1675 bytes
On 2026-10-11, Lawrence D’Oliveiro <ldo@nz.invalid> wrote:
> Here’s an example that matches the type of the match-expression (not
> its value) against various event subclasses:
>
>     events = iter(self.ws.events())
>     while True :
>         event = next(events, None)
>         if event == None :
>             break

Um.

Why would you do that and not:

    for event in self.ws.events():

?
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads