12 points joshcsimmons 1 day ago 5 comments

binaryturtle 33 minutes ago | parent

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.

robmccoll 28 minutes ago | parent

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.

sunrunner 15 minutes ago | parent

Is this essentially the mechanism behind Duff's Device (switch cases being better thought of as line-oriented jumps instead of structured blocks) but generalised?

mtklein 3 minutes ago | parent

sort of like this,

    #include <stdio.h>

    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, &f));) {
            printf("generator yielded %g\n", (double)f);
        }
        return 0;
    }
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.

quietbritishjim 4 minutes ago | parent

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.