C’s designated initializers give us named arguments
14 points by nytpu
14 points by nytpu
the author of the article is a known rapist who got caught with child porn years ago. im not sure what the criteria for banning websites is on lobsters. but it'd be lovely if i could never see his name or blog again.
I think twitch streamer Tsoding is using this pattern a lot in his C projects, or at least that's where I've seen it the first time. Here it is in his nob.h helper.
This is even nicer in C++. When you do this in C++, your arguments class can provide default values for everything, so unspecified values are default initialised to something sensible. It's also easier to version because you can create a new version of the structure and have it in the header with different overloads in the library. For example, v1 of your header looks like this:
struct ArgumentsV1
{
bool isEnabled = true;
};
void do_the_thing(const ArgumentsV1 &);
A caller does do_the_thing({.isEnabled = false}) or do_the_thing({}) and they get the expected behaviour. Now you realise you want another argument, so you do:
struct ArgumentsV2
{
bool isEnabled = true;
int maximumThings = 42;
};
// Implemented in the library but *not* exposed in the header
//void do_the_thing(const ArgumentsV1 &);
void do_the_thing(const ArgumentsV2 &);
Any new caller will see the new overload for do_the_thing and will provide both parameters. Any old callers will use the old overload of do_the_thing, which you can implement internally creating the new default structure and then initialising all of its fields with the ones from the V1 arguments structure.
Each overload of do_the_thing has a different mangled name, so the original version is still there for existing callers compiled with the old version of the header, but it isn't present in the header and so doesn't participate in overload resolution and there's no ambiguity about what the initialiser should be creating. A new caller with the new header will see only the V2 version and so their do_the_thing({.isEnabled = false}) is equivalent to do_the_thing({.isEnabled = false, .maximumThings = 42});.
So you get default and optional parameters. The only slightly annoying thing in C++ is that the parameters must be passed in order, so do_the_thing({.maximumThings = 42, .isEnabled = false}); is invalid. You can omit options in the middle though, so this is fine: do_the_thing({.maximumThings = 12}).
But I agree with @sarah-quinones. It would be great to block this domain. This is far from the only write up of this feature. I didn't click on the link, but I've read some very good writeups of how to use this over the last five+ years elsewhere (including at least one linked to from here).
This is nice and you even get ABI compatibility, which in C++ is quite tricky to obtain.
C++ gives you a lot of tools to maintain stable ABIs, but it really doesn't like to make them the default.
One of the things I keep pondering doing is writing a paper with the title 'Library boundaries are real but they can't hurt you'. C++ has two concepts of granularity: a compilation unit (source file + all headers it includes) and a program. But real software has additional boundaries. Swift, for example, explicitly acknowledges the existence of libraries and so can do something that looks like a C++ template within the library but a type-erased wrapper that looks more like a C++ class with virtual methods across the boundaries. C++ can't express something like that because there's no difference between two compilation units in the same library and two in different libraries. You can build something like this with conditional compilation and build-system macros, but its an extra hack layered on top. This showed up in C++11 when thread-local objects were allowed to have constructors and destructors, which is impossible to implement in a way that is both efficient and non-confusing in the presence of dynamically loadable shared objects, but that's fine because dynamic loading is not a concept that exists in the C++ abstract machine at all.
This is basically what the convention in zig is also. A common pattern is to define an "options" struct you pass to an initializer, this also allows you to define default values for those arguments. Zig's anonymous struct syntax makes this quite nice.
It's really funny that this seems to be an independent development of the exact same idea used in Rust (see here: https://lobste.rs/s/bvrhgq/arguing_about_arguments)
Except this correctly acknowledges it as kinda a clunky hack; while e.g. that linked Rust article has a strong air of "here's why this thing that could tangibly be more convenient is good actually and you shouldn't complain about it" (note I'm not actually arguing Rust nor C should actually have named arguments because there's good reasons they don't; just funny seeing others' arguments about it)
One nice pattern that has shown up for me a few times is that you can reuse the options struct for things that build upon what it's for. e.g. CopyString uses the same options struct as PushSize and just passes it through to the raw function.
My only real gripe, and it's admittedly small, is that when compiling with gcc you need to globally disable -Woverride-init since gcc doesn't support surrounding the options parameter with pragmas like clang does. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60875
Never heard of this, this is so cool. The thing with these tricks in C is that I am often tempted to just use it everywhere, which most of the time ends up not being the right way of doing it. Does anybody have any guidelines on when to use this vs normal function arguments?
It’s fine any time it makes sense in your program. Every compiler in the world is going to stack-allocate the space, and then pass a pointer to the struct under the hood. So it’s not 0 cost in that fields can’t then go directly in registers but it’s super cheap in general. I actually automate this with a janky preprocessor; C compound struct literals allow you to specify fields multiple times, so you can even have default values really trivially at no extra cost (compiler will optimize out trivial defaults that get re-used).