3 comments

  • quietbritishjim1 minute ago
    C++&#x27;s version of this is more verbose but still sometimes worth it: make the caller instantiate the struct and initialise it as separate statements:<p><pre><code> FooParams fooParams; &#x2F;&#x2F; Gives sensible defaults to all members fooParams.x = 1; fooParams.y = &quot;xyz&quot;; foo(fooParams); </code></pre> It doesn&#x27;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&#x27;s even more annoying because you need to move to the function argument still which looks odd when your goal is to copy overall.
  • binaryturtle29 minutes ago
    I always thought Amiga&#x27;s &quot;tag list&quot; concept was really nice. You easily could extend the API w&#x2F;o breaking the ABI, which was essential for its shared library concept. And it essentially gave you named arguments too.
  • robmccoll24 minutes ago
    It&#x27;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.
    • sunrunner11 minutes ago
      Is this essentially the mechanism behind Duff&#x27;s Device (switch cases being better thought of as line-oriented jumps instead of structured blocks) but generalised?
      • mtklein0 minutes ago
        sort of like this,<p><pre><code> #include &lt;stdio.h&gt; static int generator(int st, float *out) { switch (st++) { case 0: *out++ = 1.0f; return st; case 1: *out++ = 7.0f; return st; case 2: *out++ = 9.0f; return st; } return 0; } int main(void) { float f; for (int st = 0; (st = generator(st, &amp;f));) { printf(&quot;generator yielded %g\n&quot;, (double)f); } return 0; } </code></pre> but notice you also have the option to make the state static rather than a parameter, which gives you more encapsulation of your state machine at the cost of being way less flexible in terms of threading and reentrance.