Article View: comp.lang.c
Article #652Re: Taming The Switch-Statement
From: Dan Cross
Date: Wed, 21 Oct 2026 15:37
Date: Wed, 21 Oct 2026 15:37
117 lines
5739 bytes
5739 bytes
In article <11baiuc$11ac$1@dont-email.me>, bart <bc@freeuk.com> wrote: >On 21/10/2026 12:21, David Brown wrote: >> On 21/10/2026 03:04, bart wrote: >>> Here, one problem is that there are 3 types that I don't know and >>> would have to look up. >> >> Incorrect. > >If I wanted to use function from a language that is not C and for which >nobody has yet created a set of bindings, then I would have to >investigate every single abstract type. That is true of every programming language. >Even then, I might find that a year later, 'err_t' has been defined to >be something slightly different. Standard types are easier to deal with >and more likely to be stable. That is also true of every programming language. You can write some library in BartM or whatever language you like, and someone might write a binding to call it from, say, Pascal; and then a year down the road, you might change your compiler (since I gather you don't bother with a well-defined ABI, based on your not seeming to understand them) or change the type of something in a record, and you don't tell the Pascal person; now, they've got a program that will probably link against your library, and may even work correctly _most of the time_, but sometimes will just mysteriously crash. Welcome to programming. It's Hard(TM). Your answer to that appears to be to simply give up on abstraction; others work around it by writing tools (cf, `swig` or `bindgen` for Rust). >>> That signed char return is unusual; why not 'int'? (How would you even >>> print an 'err_t' value anyway)? >> >> It seems that yet again, you need an explanation of fundamental points >> of software development. Today's lesson is on "abstraction". > >OK. I've just looked at several graphics libraries. Those have chosen to >use predominantly standard C types for function return values. And >standard C types for numeric arguments. > >Sometimes they might be prefixed, eg. GL_int, SDL_bool, probably as >abstraction from the vagaries of standard C. > >It seems a trend to invent pointless extra types for everything. In C >itself, there are about 35 denotations for integer types, and that's >before you get into clock_t, time_t, offset_t and everything else. > >In general most boil down to one of i32 u32 i64 u64. Judicious use of types can render entire categories of errors unexpressible. Alexis King wrote a wonderful post about this, expressed using Haskell, but the general principles apply more broadly. https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/ >So you can go over the top with abstraction. Of course. Part of becoming a true software engineer is learning to find balance between too much abstraction and too little. >> Maybe on some targets (of this portable library), err_t is "signed >> char". > >Why not define it on top of 'int'? Is it likely that large numbers of >error codes are going to be stored within structs and arrays so that the >size must be minimal? Because maybe on some platforms it's more efficient to keep it in a `signed char`. Who knows? The point is you almost never need to care. >> It is also nice to know that it can be used in a switch, and cast to an >> int for printf. > >How would you know that? I thought it was abstract! Maybe err_t is a >small struct. Because the documentation about it tells you that you can: "The type `err_t` is used to represent errors. It can be cast to `int`." Really, this is table-stakes software development. >> Or look at the "ip_addr_t" type. It happens, at the moment, to be a >> struct. But it might have been a union. The size and contents of the >> struct depend on details of how the library is built - whether it has >> support for ipv4, ipv6, or both. Maybe a future version will also >> support ipv8, or ipv5 (I've heard some people are trying to make an IP >> protocol that covers more than IPv4, but without most of the >> complicating additional features of IPv6). It is an abstract type - you >> use it without knowing the details. > >I haven't said much about that. It is a struct so some username is >needed. I renamed it to IP_addr in my example. Huh? Username? >But I'm not sure it needs the _t suffix. (Is it a thing now for every >typedef name to need _t?) > >I notice however that the argument is a pointer: if ip_addr_t* values >are commonly passed around, I would put an abstraction around that. > >(However that's partly because how my language works is different; to >access a member of the struct, I don't need to know if the type of X is >an actual struct or is a pointer to one; I write X.addr in both cases.) So does Rust. You're welcome. - Dan C.
Technical Details
Message-ID:
<11bam77$6at$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-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11b933e$3h3d0$1@dont-email.me> <11ba78m$3peq3$3@dont-email.me> <11baiuc$11ac$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 15:37:11 -0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: <11bam77$6at$1@reader2.panix.com> References: <119qf6l$2rmg3$1@dont-email.me> <11b933e$3h3d0$1@dont-email.me> <11ba78m$3peq3$3@dont-email.me> <11baiuc$11ac$1@dont-email.me> Injection-Date: Wed, 21 Oct 2026 15:37:11 -0000 (UTC) Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80"; logging-data="6493"; mail-complaints-to="abuse@panix.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: cross@spitfire.i.gajendra.net (Dan Cross)