News for hackers. Stuff that matters.
Re:Chaining, or why you should stop returning void (2012) by flohofwoe on Sunday November 07, 2021 @09:59AM (#29139941)
The "Galaxy Brain" version of this advice is using plain old designated initialization of 'option bag' data structures. Here's a non-trivial C99 example from the sokol_gfx.h API to create a pipeline-state-object for 3D rendering:

    state.pip = sg_make_pipeline(&(sg_pipeline_desc){
        .layout = {
            .attrs = {
                [ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
                [ATTR_vs_color0].format   = SG_VERTEXFORMAT_FLOAT4
            }
        },
        .shader = shd,
        .index_type = SG_INDEXTYPE_UINT16,
        .cull_mode = SG_CULLMODE_BACK,
        .depth = {
            .write_enabled = true,
            .compare = SG_COMPAREFUNC_LESS_EQUAL,
        },
        .label = "cube-pipeline"
    });
Chaining might be useful for data processing pipelines, but not for simple data initialization tasks IMHO.
Re:Chaining, or why you should stop returning void (2012) by matheusmoreira on Sunday November 07, 2021 @12:46PM (#29141649)
Designated initializers is the most amazing feature of the C language. I don't understand why no other language ever copied it...
Re:Chaining, or why you should stop returning void (2012) by kevinmgranger on Monday November 08, 2021 @08:42AM (#29149164)
Didn't Swift adopt something similar? (I'm no Swift dev so I'm not sure)
Re:Chaining, or why you should stop returning void (2012) by AtlasBarfed on Sunday November 07, 2021 @01:42PM (#29142187)
Groovy has this ability I think if I can parse that code. My C code experience predates C99
Re:Chaining, or why you should stop returning void (2012) by jmercouris on Sunday November 07, 2021 @12:58PM (#29141769)
It exists in Lisp, even before C.
Re:Chaining, or why you should stop returning void (2012) by jimbob45 on Sunday November 07, 2021 @11:07AM (#29140575)
I know C is an extremely deep language but that doesn’t look like C99 to me. Since when can you set struct member variables piecewise in a…function definition?

Is this maybe a custom compiler? Or is this actually C++?

Re:Chaining, or why you should stop returning void (2012) by MayeulC on Sunday November 07, 2021 @12:07PM (#29141230)
As other commenters have pointed out, those are designated initializers[1], one of my favorite C99 feature.

I's also been picked up recently for inclusion into C++20[2].

However, it seems that C++ adds a specific limitation:

> all designators used in the expression must appear in the same order as the data members of T

> out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++

Which limits the usefulness, as I've used this syntax to let myself reorder struct fields (and add some) without breaking the API (while obviously breaking the ABI). It's a great tool to use while your API is still in flux.

At least an error will be produced, which is much better than just providing values and hoping that the API/ABI remains stable.

[1]: https://en.cppreference.com/w/c/language/struct_initializati...

[2]: https://en.cppreference.com/w/cpp/language/aggregate_initial...

Re:Chaining, or why you should stop returning void (2012) by kevin_thibedeau on Sunday November 07, 2021 @11:26AM (#29140795)
It's a C99 compound literal with designated initializer. C++ doesn't support it.
Re:Chaining, or why you should stop returning void (2012) by rackjack on Sunday November 07, 2021 @11:23AM (#29140758)
It is C99, which surprised me too.

https://stackoverflow.com/a/7487949/11736447

Re:Chaining, or why you should stop returning void (2012) by chisquared on Sunday November 07, 2021 @11:23AM (#29140760)
I encountered code like this in a C99 project recently. I still don't understand it, but I believe it's supposed to be possible.
Re:Chaining, or why you should stop returning void (2012) by nwellnhof on Sunday November 07, 2021 @11:22AM (#29140753)
What you're seeing is a compound literal with designated initializers. Both features were introduced in C99.