🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #650

Re: Taming The Switch-Statement

#650
From: David Brown
Date: Wed, 21 Oct 2026 15:29
146 lines
7488 bytes
On 21/10/2026 15:22, Dan Cross wrote:
> In article <11baah2$3te3v$1@dont-email.me>,
> David Brown  <david.brown@hesbynett.no> wrote:
>> On 20/10/2026 21:24, Dan Cross wrote:
>>> In article <11b7k22$2v8e5$1@dont-email.me>, bart  <bc@freeuk.com> wrote:
>>>> [snip]
>>>> (There was a LOT of antipathy towards my idea to fix the common case of
>>>> 'loop-break' clashing with 'switch-break', BECAUSE it did not fix the
>>>> problem of nested loop-break, which is uncommon, even though that wasn't
>>>> the aim.)
>>>
>>> Those are two different things.  No one disagrees with you that
>>> "nested break from a switch inside of a loop" is relatively more
>>> common than other variants.
>>
>> Quibble - that is not established to my satisfaction.  I am not
>> convinced that wanting a nested break for "switch inside loop" is more
>> common than wanting it for "loop inside loop".  So I neither agree nor
>> disagree with that claim.  I don't, however, think it matters much -
>> both are situations that turn up sometimes in code, and definitive
>> answers are neither possible nor useful.
> 
> Ok, that's fair; I shouldn't say "no one disagrees".

And I didn't say I /did/ disagree - merely that I don't feel I have the 
information to disagree or agree.

We all over-generalise, and that's always bad!

> 
>>> [snip]
>>> There are plenty of languages that can do this kind of thing
>>> that are not C and have syntax and semantics closer to what you
>>> seem to want.  Here's an example in Rust:
>>>
>>> pub trait TimerHandle { ... }
>>>
>>> fn CreateTimer<T>(
>>>       timer_name: &str,
>>>       period: std::time::Duration,
>>>       auto_reload: bool,
>>>       timer_id: T,
>>>       handler: fn(&dyn TimerHandle),
>>> ) -> Result<Timer<T>, TimerError>
>>>
>>> I didn't know what an `UBaseType_t` is, and while "autoreload"
>>> sounded pretty intuitive, I wasn't sure about the semantics, so
>>> I looked it up (the example comes from `FreeRTOS`).  In this
>>> context, it is a boolean saying whether the timer should repeat
>>> with the given periodicity, or whether it is a one-shot.
>>
>> FreeRTOS is written in C90 - a mistake, IMHO - and thus does not use
>> bool.  Instead, it uses constants pdTRUE and pdFALSE of type
>> UBaseType_t.  UBaseType_t is a type that is dependent on the target -
>> usually it is either 16-bit or 32-bit.  But of course it doesn't matter
>> what the underlying types are - you use the typedef names, and the
>> documented constants.
> 
> Thanks for the context.  Your point that the underlying type
> doesn't matter is salient: it's opaque to the programmer, but
> 99% of the time the programmer doesn't need to worry about the
> details.

Yes.

> 
>>> The timer_id is opaque to the timer, and it looks like it's used
>>> to facilitating the handler accessing timer-specific data.  In
>>> C, passing it as a `void *` is used as a way to get a poor-man's
>>> overloading.  But Rust has generics, I just made it generic over
>>> some type T.
>>
>> FreeRTOS uses "void *" to mean "pointer to internal structure that you
>> shouldn't be messing with".  It uses this far too often, and is, IMHO, a
>> poor design choice.  (I also dislike the silly Hungarian naming.)  I
>> would much preferred if there had been :
>>
>> 	struct Internal_Timer_Struct;
>> 	typedef struct Internal_Timer_Struct * timer_id_t;
>>
>> and then the parameter "timer_id_t timer_id".  So in a re-write, it
>> would not be a generic.  (You might choose to use a generic for the
>> handler function type, so that handler callbacks could have parameters
>> of other types, but not for the timer id.)
>>
>> I am using these declarations from FreeRTOS (and LWIP) to demonstrate
>> the use of function typedefs in massively popular codebases, not to
>> claim FreeRTOS is a paradigm of C code design!
> 
> Oh interesting; in this case, I had assumed it was treated as
> a callback-specific type so one could plumb extra context into
> the handler; the documentation I saw suggested it could be used
> by the handler to disambiguate several timers reusing the same
> handler function.

Actually, I have mixed these up, and you are correct - the void* id is 
user-assigned, and you use it as you wish.  There are other places in 
FreeRTOS where pointer types get "erased" and reduced to void* 
unnecessarily, but this was not one of them.  (In my poor defence, the 
FreeRTOS code I have doesn't use timers, and the bits it does use - 
threads and queues, mainly - is abstracted to a C++ interface.)

> 
>>>   For example:
>>>
>>> [snip]
>>>
>>> Note this is decently complex behavior, but it's actually quite
>>> intuitive from the name and the type.
>>
>> You have got a very clear picture here, much of it from the names of the
>> types.  For a Rust programmer, you give a good impression of experience
>> with C :-)
> 
> I've been known to dabble.
> 
>> The only mistake you made is that programmers working on systems using
>> FreeRTOS and the like, have a fear and loathing of "malloc", have never
>> heard of "abort()", and do not join our threads because they all run
>> until the power is switched off.
> 
> Ha, fair; that was just a made-up example, based on other thread
> libraries I have used (in C).  I didn't mean for it to be
> interpreted strictly in the context of FreeRTOS! :-D
> 
>> [snip]
>>
>> Or just have :
>>
>> 	fnptr pc = first;
>> 	fnptr *pcptr = &pc;
>> 	
>> I don't see a need for "uintptr_t", except so that you have to have
>> casts like Bart had.
> 
> Yes.  But I wanted to remain faithful to his original, to show
> that one could write a more-or-less exact transliteration in C
> that was extremely similar to his chosen syntax.
> 
>>> [Snip a bunch of Rust code; the point of which being that
>>>   bart need not complain about lack of an alternative to C]
> 

It was interesting to see the Rust code, but it made one thing clear to 
me - Rust should be used for writing Rust code, and not for writing C 
code in Rust syntax.
Technical Details
Message-ID: <11balos$1042$2@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> <11b7k22$2v8e5$1@dont-email.me> <11b8f5h$67l$1@reader2.panix.com> <11baah2$3te3v$1@dont-email.me> <11baeai$959$1@reader2.panix.com>

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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 21 Oct 2026 17:29:32 +0200
Organization: A noiseless patient Spider
Lines: 146
Message-ID: <11balos$1042$2@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11b7k22$2v8e5$1@dont-email.me>
 <11b8f5h$67l$1@reader2.panix.com> <11baah2$3te3v$1@dont-email.me>
 <11baeai$959$1@reader2.panix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 21 Oct 2026 15:29:35 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="32898"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18bZcymrdXtlnQfNiUm5j0JrU19lM/6g7w="; posting-host="f78456a6491f23b3f53847b5a3287905"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:1njm+N0uuxQ8OEJfSLIeJV9m9Ic= sha256:kCkhTIutuGEf46Og0dIz1sn5cWCewZ0tCdp6GSi01nw=
	sha1:jKZC6F0sL6HmWkxmaCdahVlhKgY= sha256:CqJ7X75VeddXnbRYrHExcmA+ELG/u+Fm/oUiWweK6IQ=
Content-Language: en-GB, nb-NO
In-Reply-To: <11baeai$959$1@reader2.panix.com>