Article View: comp.lang.c
Article #361Re: Taming The Switch-Statement
From: Lawrence D’Oliveiro
Date: Tue, 13 Oct 2026 22:00
Date: Tue, 13 Oct 2026 22:00
36 lines
2612 bytes
2612 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
Technical Details
Message-ID:
<11am9m9$ul56$3@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-mailReferences:
<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: Tue, 13 Oct 2026 22:00:41 -0000 (UTC) Organization: A noiseless patient Spider Lines: 36 Message-ID: <11am9m9$ul56$3@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: Tue, 13 Oct 2026 22:00:42 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1004710"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FPoUEHNsxgzt64qQK7Xyy"; posting-host="f50919a0ed08414b1012e34ff88483da" User-Agent: Pan/0.165 (Kostiantynivka) Cancel-Lock: sha1:EMSG8H4iIsFRmCVqBe8Aiw8GE/Q= sha256:2po/SC3eW6H2sjrp9qC40Kg5MiCy2q4AwPIeQjg3j4s= sha1:dGX2BYEmzKURImGecsVOK7DVUDA= sha256:qZlYgY3VKNfo5b/Io2kEo4lBwaoAbh9NEIiy3ThLJs8=