Hacker News new | past | comments | ask | show | jobs | submit login

Or you could just use functions to do the same thing.

I assure you you did not invent building linked lists on the stack.

By the way, you should be wrapping your giant macros (when they are necessary and useful; they aren't here) in do {} while (0) blocks.




Now you made me curios. How would you go about doing it with functions? Keep in mind that the goal is to interleave conditions and the code in switch-like way.


The goal is to implement the desired functionality in a comprehensible way. Given that, the best option would be to use an array, and to initialize it with the easy array intialization syntax that C provides:

    struct select_condition conds[] = {
        { channel_1, SELECT_OUT, NULL, 42 }, 
        { channel_2, SELECT_IN, &i, 0 }
    };
No need to overcomplicate it or stuff a bunch of functionality into macros in order to make C look like some other language.


Seems like the 'classic' C way to go about it would be to use scanf/printf-style format strings:

    int mode = select("<%i | >%i", channel1, 42, channel2, &i);
    switch(mode) {
    case 0: foo(); break;
    case 1: bar(i); break;
    }


The goal is not to look like golang but to maintain locality: Implementation of one thing should be at one place, not at two different places.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: