Article View: comp.lang.c
Article #1696Re: Inversion-of-control and dependency-injection in C
From: Ross Finlayson
Date: Fri, 20 Nov 2026 13:48
Date: Fri, 20 Nov 2026 13:48
185 lines
11998 bytes
11998 bytes
On 11/20/2026 04:35 AM, Tristan Wibberley wrote:
> On 16/11/2026 18:09, Bonita Montero wrote:
>> Am 13.11.2026 um 23:50 schrieb Ross Finlayson:
>>
>>> So, here's one. Say you got a server process for sockets of the
>>> connection-oriented or datagram-wise or association-oriented like
>>> TCP, UDP, or SCTP, and it manages the connecting and the sending and
>>> receiving about a million of those and is sort of fixed-size and
>>> stable in a usual runtime. So, how do you design the interface,
>>> about stuff like the state-machine of _protocols_, all about
>>> _protocols_, in the application-layer and above, say, for example
>>> the incremental-parsing and the state-machine of the protocol
>>> and sub-protocols and upgrade-protocols and the like, what sort
>>> of abstraction with suitable-defaults and reasonable-opinions
>>> makes for having any sort protocol as a composable library.
>
>> Use C++20 coroutines with Bost.ASIO and io_uring. This gives
>> as less code as possible with nearly optimal I/O performance.
>
> I like this answer.
>
> It's funny: people used coroutines and CoEs back in the 1970s (I did it
> as a child, myself), then everyone told each other to stop it, now 50
> years later here we are again, finally succeeding once more after a long
> hiatus, albeit with appalling syntax and semantics everywhere around it
> like thorny bushes.
>
The re-routine, or retry-coroutine, is a simple sort of abstraction
that basically has "write code in the language that looks like
synchronous blocking code and collect & cache its intermediate results
according to encounter-order then just re-run the whole routine until
it succeeds/errors/times-out, or about cancellation/time-out one may
aver", sort of like NFA's the non-deterministic automatons vis-a-vis
DFA's the deterministic automatons that are made from flattening NFA's,
though NFA's are deterministic and should be called something else,
makes for an account that the backing-modules move around it instead
of the other way around.
It's considered simplified for reason's sake when "callback & yield",
i.e., "the primitives of asycnhronous concurrency", are simply made
implicit according to the executing-context and the
function-interception. ("..., In the language, ....")
About types in C I've been looking at it with regards to for example,
"the decaying of the array-type to the pointer-type", and particularly,
"accounts of tiered-_Generic constructs that make for function
overloading with the type-safety after type-inference of strong-types
in C". So, _Generic can work off of non-decayed complete array-types,
then for example when there are only a small number of fixed-size arrays
to care about, like BRIEFNAME = 16, FULLNAME = 256, CACHELINE = 64, PAGE
= 4096, LARGEPAGE = 2 * 1024 * 1024, then after cv-qualifiers
as part of types and complete-types, then the usual account of the
function "overloading" may result that it's inferred from the types
their constant parameters then to result that a "function:
'under'-loading" may happen, basically thusly being able to convert the
cases in the _Generic construct to derive the constant usual "len"
parameters that thusly one needn't pass the extra-parameter "len" when
dealing with arrays of fixed-size that are not decayed to pointers, then
to a single "underloaded" function with the array decayed to pointer and
the len, then that as a "compile-time invariant" the compiler is free to
optimize that according to various accounts of loop un-rolling and
vector-ization, for example in-lining the function anyways, at compile-time.
Then, these sorts of aspects of, "code-style", and "convention" see
introduced a complement concept to "function overloading", "function
under-loading", about the usual notion that "function overloading"
allows one (template) function to result emitting as many functions
as necessary disambiguated via name-mangling (name-flattening to
a flat namespace the object's symbols), instead they funnel back
to one, "specialized" for each of these few types of fixed-size
non-decaying array pointers by simply make a _Generic construct
that has "len" as a compile-time invariant, and resulting only
one symbol in the resulting object file, called variously.
Another part of what's considered this "code-style", is about
compile-time constants and "static const" variables, with regards
to what may be "constexpr" in C23 as would be by "enum" in C11
for compile-time constant that are symbolic instead of macro literals,
and then variables separately, again about arrays, and variable-length
arrays in C, having two copies of a "constant", one a constexpr the
other static const, then for "variable-length arrays", if not in
C++ then optional in C, for small buffer in un-likely branches like
formatting brief logging messages or string functions that need a
buffer, that again the _types_ in C are observed/respected (and expected).
So, in this approach to "code-style" for "inversion-of-control
and dependency-injection in C", then there's basically the _Generic
construct, which makes for a controlling-expression (evaluated as
an expression then for its _type_) the result replacement expressions or
a default (which for example may everywhere be the default expression a
static-assertion that at compile-time composes an error-message saying
what's missing by its name and _type_), then with
a commonality and distinctions among "introductions" (introduced
in quotes, i.e., add it to the glossary), of "tiered-_Generic"
constructs, starts seeing a number of approaches to making for
the basic abstraction of functions as a _boundary_, that besides
the usual account a function returning a value, the _co-variance_,
usually of value to value/status/error. Then, this sort of _boundary_
establishes responsibilities of "modules" as they would be or
"libraries" as for "components", what then have the basic account
of co-variance as: "holders, of return-types & faults, and passers, of
parameter-types their values", over and back the _boundary_, while
yet for mostly in the code, that the holders "hide & hoist" the
return-values, while, the passers "present & promote", the parameters
(as basically an account of "holders", for example "optional").
Then, where C++ templates is a great and fantastic thing for
function-overloading, and these tiered-_Generic constructs still have
explosions in definitions what result, it's yet also so that at
compile-time: the libraries (their invocations after over/under-loading)
can have their invoctions resolved and compiled _faster_ this way, not
that it's better for readability of reducing source-code
boilerplate-bloat, yet though "specializing for templates" would be
about on the same order, that because it's closed in the tiered-_Generic
constructs instead of the C++ compiler making an outer-product of the
AST-world and then pruning that down to the specialized overloads, the C
(or, C++, _Generic is a valid construct in C++) account of this way, can
result both faster compilations, then as above: faster code.
So, accounts of "code-style" and "convention" often vary (for example,
they often _don't_ and are considered very "opinionated" in their
interpretation, for example by tabs-vs-spaces or pretty-printers
and re-formatters, touching first even on the "cosmetic" before even
getting into accounts like the above of the "ergonomic"). For example,
many prefer to adopt the code style that their editor gives the cosmetic
then into the case-convention of identifiers and so on, for example
PascalCase and camlCase and SNAKE_CASE and snake_case, then for
example the ordering of declarations according to scope or file
encounter lexicographically, by then entering accounts of the language.
So, sometimes accounts of "code-style" are like, for example, "there are
no boundaries and there are no exceptions", vis-a-vis, there are
boundaries and there are exceptions, then, for example, conventions
or patterns, like "we only use co-routines and don't use executors",
vis-a-vis, "our co-routines are run by threads that are our executors",
and then the strong-coupling, vis-a-vis the weak-coupling, which
basically in terms of the concrete and the abstract, has that
"inversion-of-control and dependency-injection" needs everything at
least abstractly abstract, vis-a-vis "boundaries" as above, then though
with a goal that at run-time (or evaluation) it yet results "as concrete
and direct as possible", that leading to a usual notion
of "zero-cost-abstraction", and these kinds of things.
Why care? Not for me to say, except for myself, and for expectations
of library authors and component designers for fair boundaries yet
un-obtrusiveness. Here though is an example, these "tiered-_Generic
constructs" with these concepts of "underloading" for example or
for "vanishing/phantom types" only in the templates as they would be,
run faster at both compile-time and run-time in both C and C++,
while with the guarantees of strong types according to the compiler
in both C and C++, and with the same code in both C and C++,
and being un-surprising and mapping to standard namespace types in C++.
If the best sort of style just fades away or rather is legible and
"ergonomic", for example that all these tiered-_Generic
type-interpretation introductions live in a header and the actual
sources just defined functions that the compiler complains must exist
until they're just plain functions, and they're just plain code
and functions according to the compiler and IDE, though with that
the IDE would know about the AST the _Generic construct in its
code-model, with regards "IDE vomit" and "A.I. slop", then there's
also that "the tooling" meaning tracing, debugging, and profilers,
it's all symbols to them.
(This is not "A.I.", though "AP" does have a stated opinion
that he considers me among "the A.I.". AP of course is notorious
for "universe is one giant dot", and is thought to be in gentle
retirement in the hinterlands.)
Technical Details
Message-ID:
<WJOcnajl6sIw0p32nZ2dnZfqn_udnZ2d@giganews.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.nrbbs.org!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mailReferences:
<yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <11d6vjp$qhu8$1@raubtier-asyl.eternal-september.org> <dsednTD-Fblclmr3nZ2dnZfqnPWdnZ2d@giganews.com> <11d738r$ru7a$1@dont-email.me> <11d75i7$spj0$1@dont-email.me> <11d7c8k$173nk$1@dont-email.me> <11d7fbv$1839o$2@dont-email.me> <nrecnTdvuvbACWr3nZ2dnZfqn_adnZ2d@giganews.com> <11dfgtf$3pj6i$1@raubtier-asyl.eternal-september.org> <11dpeqd$35ddj$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.nrbbs.org!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 20 Nov 2026 13:48:29 +0000 Subject: Re: Inversion-of-control and dependency-injection in C Newsgroups: comp.lang.c References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <11d6vjp$qhu8$1@raubtier-asyl.eternal-september.org> <dsednTD-Fblclmr3nZ2dnZfqnPWdnZ2d@giganews.com> <11d738r$ru7a$1@dont-email.me> <11d75i7$spj0$1@dont-email.me> <11d7c8k$173nk$1@dont-email.me> <11d7fbv$1839o$2@dont-email.me> <nrecnTdvuvbACWr3nZ2dnZfqn_adnZ2d@giganews.com> <11dfgtf$3pj6i$1@raubtier-asyl.eternal-september.org> <11dpeqd$35ddj$1@dont-email.me> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Fri, 20 Nov 2026 05:48:23 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <11dpeqd$35ddj$1@dont-email.me> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <WJOcnajl6sIw0p32nZ2dnZfqn_udnZ2d@giganews.com> Lines: 185 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-5czn6+YRsdhXrLTYQ26+L9SI2tR+WknZo/nh1Ym1ewouiPvVygnYg6RrAtNTF8DsbSPTpuSax0IlKsh!+4ms8ewp0tquFw7hfJ05Q5L9JB6D2EZaJW7a9RGzI8ImzdgkGPmHGg4QM6MbpJW9ophD3dTZH3/g X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40