Void is surprisingly useful.
Void is surprisingly useful.
Posted Sep 9, 2026 21:15 UTC (Wed) by alx.manpages (subscriber, #145117)In reply to: Void is surprisingly useful. by NYKevin
Parent article: Stabilizing Rust's never type
The problem with optional type parameters, in generic code (which is the only kind of code that has "type parameters" of any kind, optional or not), is combinatorial blowup. For each optional type parameter, we must write twice as many implementations.
Not necessarily. Here's some generic code with a single implementation:
#define rvalue(lv) ((void)0, (lv))
#define typeof_typename(T) typeof(*(typeof(T) *){_Generic(0, T: NULL, default: NULL)})
#define mallocarray(...) reallocarray(NULL, __VA_ARGS__)
#define malloc_T(n, T) rvalue((typeof_typename(T) *){mallocarray(n, sizeof(T))})
malloc_T() is implemented internally through void* (through reallocarray(3)), but exposes a type-generic API. We could say reallocarray(3) is unsafe, but the exposed API of malloc_T() is safe. And we didn't need an exponential number of implementations. But it can certainly be used with hundreds of types.
int *p = malloc_T(42, int);
long *q = malloc_T(6, long);
struct foo *r = malloc_T(7, struct foo);
Am I misunderstanding something?
