🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #645

Re: Taming The Switch-Statement

#645
From: Dan Cross
Date: Wed, 21 Oct 2026 13:22
126 lines
6076 bytes
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".

>> [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.

>> 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.

>>  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]

	- Dan C.
Technical Details
Message-ID: <11baeai$959$1@reader2.panix.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
From: cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Wed, 21 Oct 2026 13:22:26 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11baeai$959$1@reader2.panix.com>
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>
Injection-Date: Wed, 21 Oct 2026 13:22:26 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="9385"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)