🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #239

Re: Taming The Switch-Statement

#239
From: Lawrence D’Oliveiro
Date: Sun, 11 Oct 2026 21:26
82 lines
4232 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.
Technical Details
Message-ID: <11aguua$30k5e$5@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D’Oliveiro <ldo@nz.invalid>
Newsgroups: comp.lang.c,comp.lang.python
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 21:26:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <11aguua$30k5e$5@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Oct 2026 21:26:35 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3166382"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19l5Js05QuC0s39J2A4OrSR"; posting-host="a82cbb4a9e343247fd3ed93594ed57ba"
User-Agent: Pan/0.165 (Kostiantynivka)
Cancel-Lock: sha1:OTl7K+oqna4LYnAE67KlJe3t7Nc= sha256:pF8Dc5PvxcHXCcH+NcAyuf9Y8VcA5l5p165oYGBX/8U=
	sha1:MGmF+Mx8iVSdxvjQ63qb1+kwEcw= sha256:4XjcEtLypQL7cDbOKScVBcU1XwAeTI97AY6GkI4XPnQ=