Elektrine lite

← Feed

@thephd@pony.social

Post #1884816

2026-05-02 14:43 UTC

It's live. https://godbolt.org/z/71f3xxcYh #include #define bar(x) _Generic(x, \ int v: v, \ struct foo v: v.name \ ) struct foo { char* name; }; int main() { struct foo f = { "test" }; bar(3); bar(f) = "something"; puts(f.name); // prints "something" }

Replies (9)

  • @thephd@pony.social I am confused but it seems cool

    Open ##1892860

  • @fay59@tech.lgbt 2026-05-02 20:31

    @thephd@pony.social if you allowed “auto x” for a default case, `#define bar(X) _Generic(X, auto x: …)` could become the least bad way to bind the expansion of a preprocessor macro to a variable inside a single expression

    Open ##1897176

  • @thephd@pony.social 2026-05-02 14:47

    @simontatham -- not to bother you but since this is now live you can probably kick the tires a little bit on it in Godbolt. I think it solves most of your concerns (save what happens in the default: branch!). Still need to figure out if I need braces around the _Generic bits to see if it works.

    Open ##1907309

  • @thephd whoaaaa this is a big deal

    Open ##1907310

  • @thephd very cool. is it going to happen to iso c?

    Open ##1907312

  • @wren6991@types.pl 2026-05-02 15:03

    @thephd wait that's illegal

    Open ##1907315

  • @uecker@mastodon.social 2026-05-02 15:54

    @thephd I don' think we need more braces. The scope can be limited as you implemented it.

    Open ##1907318

  • @erisceleste@tech.lgbt 2026-05-02 16:45

    @thephd aw hell yea in addition to the primary use, im super excited for an alternative usage it enables which ive wanted for many years: #define let_in(TMP, VAL, EXP) _Generic((VAL) \ , typeof (VAL) TMP: (EXP)) // temporary variables within expressions! int x = let_in(tmp, side_effects(), tmp > 10 ? tmp : tmp - 1); finally you can write a correct max macro lol (what i think of as "lightweight lambdas" - all i want is a name binding, don't need the complexity of anything bigger)

    Open ##1907322

  • @thephd ... wait what the hell is happening here? is this like a factory for creating new structs, except the structs are referred by number?

    Open ##2027365