Hacker news

  • Top
  • New
  • Past
  • Ask
  • Show
  • Jobs

C's designated initializers give us named arguments (https://quuxplusone.github.io)

72 points by joshcsimmons 2 days ago | 45 comments | View on ycombinator

quietbritishjim about 20 hours ago |

C++'s version of this is more verbose but still sometimes worth it: make the caller instantiate the struct and initialise it as separate statements:

   FooParams fooParams;  // Gives sensible defaults to all members
   fooParams.x = 1;
   fooParams.y = "xyz";
   foo(fooParams);
It doesn't work so well if you want to move construct things because you need to move at least twice: once when moving the value into the member, then you need to move the whole struct to the function argument `foo(std::move(fooParams))`. If you want it to copy something but avoid unnecessary copying then it's even more annoying because you need to move to the function argument still which looks odd when your goal is to copy overall.

binaryturtle about 20 hours ago |

I always thought Amiga's "tag list" concept was really nice. You easily could extend the API w/o breaking the ABI, which was essential for its shared library concept. And it essentially gave you named arguments too.

s3graham about 16 hours ago |

Love it too! https://mstdn.social/@sgraham/112486947826240650

As I mentioned there, I think it's even cooler when you collapse the "constructor" macro, and tag to be the same name.

cryptonector about 19 hours ago |

I proposed something like this ~20 years ago in the context of GSS-API, where there are several functions with lots of arguments but where most arguments have reasonable defaults, so one could use named arguments for all the values that one must give and not mention the others that get default values. But I wasn't serious about it.

david2ndaccount about 18 hours ago |

I always considered this kind of a hack, needing to wrap the actual function call with a function-like macro. Moreover, you’re now using a different calling convention with passing a struct instead of basic types which can be less efficient. I wish C would add actual named arguments like I added in my DrC project: https://github.com/drpriver/DrC/blob/master/EXTENSIONS.md#na...

cozzyd about 18 hours ago |

The idiom I usually use is to define a DEFAULT_FOO_PARAMS like

  struct foo { int Z; int A; double E, double v }; 
  #define DEFAULT_FOO_PARAMS  .Z = 1, v = 3e8 

  foo_fn ( &(struct foo){ DEFAULT_FOO_PARAMS, .Z  =2, .A = 4, .E = 1 });

But I do sort of wish there was a language-supported way to set defaults for a designated initializer in the struct definition (like C++ default values) to avoid having to write DEFAULT_FOO_PARAMS there.

robmccoll about 20 hours ago |

It's fun watching people rediscover this every so often. If you like this sort of thing, go check out creating generator functions using a switch embedded throughout your function. You can wrap it in macros to end up with a nice yield keyword emulation.

jcalvinowens about 17 hours ago |

The macro is cute, but passing structures by value like that is going to cause real measurable overhead where the functions don't get inlined (the author sort of admits this I guess by calling it "heavyweight").

flohofwoe about 19 hours ago |

Tbh I would skip all the preprocessor magic proposed in the article and just go for plain C99 'option bag struct' arguments, the called function only needs to be aware that "zero means default".

I've been using this style for almost 10 years now in the sokol headers, and it still feels like the right thing to do and actually more ergonomic than most other languages for initializing complex structs, e.g. see here for creating a 3D render pipeline object with dozens of creation options, but most of them keeping their default values:

    state.pip = sg_make_pipeline(&(sg_pipeline_desc){
        .layout = {
            .attrs = {
                [ATTR_texcube_pos].format = SG_VERTEXFORMAT_FLOAT3,
                [ATTR_texcube_color0].format = SG_VERTEXFORMAT_UBYTE4N,
                [ATTR_texcube_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
            }
        },
        .shader = shd,
        .index_type = SG_INDEXTYPE_UINT16,
        .cull_mode = SG_CULLMODE_BACK,
        .depth = {
            .compare = SG_COMPAREFUNC_LESS_EQUAL,
            .write_enabled = true
        },
        .label = "texcube-pipeline"
    });
(or see here: https://github.com/floooh/sokol-samples/blob/11bbe1a6865e93c...)

...this code snippet also makes use of C99 features that are not available in C++20 though (or most other languages), and it's really remarkable how well designed the C99 designated init feature as a whole is (even though it was an afterthought) and how badly its individual subfeatures were understood by most later languages (basically, only Odin got it "mostly right": https://github.com/floooh/sokol-odin/blob/7d47a31cad83a26ba1...)

nixpulvis about 17 hours ago |

Treating named arguments as structs is my proposal for Rust as well: https://nixpulvis.com/ramblings/2026-10-19-anonymous-struct-...

cesaref about 17 hours ago |

I do wonder how many times setting the same member twice is a bug rather than a feature. I can imagine quite a bit of copy/paste leading to errors like this.

coldpie about 19 hours ago |

I both love & hate this. I would genuinely consider using it in my own projects, and also I would be very pissed off if I encountered it in someone else's project. Brilliant.