🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.python
1 messages
1 total messages Started by Lawrence D’Oliveiro Tue, 13 Oct 2026 22:00
Re: Taming The Switch-Statement
#2
Author: Lawrence D’Oliveiro
Date: Tue, 13 Oct 2026 22:00
36 lines
2547 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.

Another, slightly more involved example, where the matching is against
the components of a tuple. This is from a parser for the Varlink
interface definition language. Note the use of the “_” wildcard to
indicate don’t-care parts of matches:

    match token.value, token.rule_name :
        case "[]", _ :
            nexttoken()
            valtype = parse_typedef(None)
            result = ArrayType(name, valtype)
        case "[string]", _ :
            nexttoken()
            valtype = parse_typedef(None)
            result = DictType(name, valtype)
        case _, "elttypedef" :
            result = parse_elttypedef(name)
        case _, "typedef" :
            result = parse_typedef(name)
        case _ :
            raise AssertionError \
              (
                    "unrecognized typedef token %s: %s"
                %
                    (token.rule_name, repr(token.value))
              )
    #end match
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