• Re: Taming The Switch-Statement

    From Lawrence D?Oliveiro@3:633/10 to All on Sun Oct 11 21:26:34 2026
    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.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Jon Ribbens@3:633/10 to All on Sun Oct 11 22:12:38 2026
    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():

    ?

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to All on Tue Oct 13 22:00:41 2026
    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

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)