🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #363

Re: Taming The Switch-Statement

#363
From: bart
Date: Tue, 13 Oct 2026 22:19
62 lines
3497 bytes
On 13/10/2026 23:00, Lawrence D’Oliveiro wrote:
> 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

This is not quite structural pattern matching. Isn't this just 
equivalent to:

    match token.value:
       case "[]":
           nexttoken()
           valtype = parse_typedef(None)
           result = ArrayType(name, valtype)
       case "[string]":
             nexttoken()
             valtype = parse_typedef(None)
             result = DictType(name, valtype)
       else:
           match token.rule_name:
               case "elttypedef":
                   result = parse_elttypedef(name)
               case "typedef":
                   result = parse_typedef(name)
               else
                   .... error

Except that 'else' probably doesn't work like this (why not).

Technical Details
Message-ID: <11amaoj$v8qm$1@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> <11am9m9$ul56$3@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: bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Tue, 13 Oct 2026 23:19:00 +0100
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <11amaoj$v8qm$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me>
 <11am9m9$ul56$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 13 Oct 2026 22:19:00 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1024854"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/TAu+3Zu/3yFgCa8VolHut"; posting-host="a4fe58cca8661e75894507eaca96fb60"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:TPuW7mISAN0WiOqllBe4c/c2ZR8= sha256:bcnrBajb0NWQ7Yc86nXRAE0gBVYSnVukcunAcpeI3hU=
	sha1:9tbSXvZ+qJ1M+or+uIeYUNuEtZg= sha256:dqWsXrKtnt2Nb1jjgOHPq4TVPHVHc3hcp+nS/Njdll8=
In-Reply-To: <11am9m9$ul56$3@dont-email.me>
Content-Language: en-GB