72 points by joshcsimmons 2 days ago | 45 comments | View on ycombinator
quietbritishjim about 20 hours ago |
binaryturtle about 20 hours ago |
s3graham about 16 hours ago |
As I mentioned there, I think it's even cooler when you collapse the "constructor" macro, and tag to be the same name.
cryptonector about 19 hours ago |
david2ndaccount about 18 hours ago |
cozzyd about 18 hours ago |
struct foo { int Z; int A; double E, double v };
#define DEFAULT_FOO_PARAMS .Z = 1, v = 3e8
foo_fn ( &(struct foo){ DEFAULT_FOO_PARAMS, .Z =2, .A = 4, .E = 1 });
But I do sort of wish there was a language-supported way to set defaults for a designated initializer in the struct definition (like C++ default values) to avoid having to write DEFAULT_FOO_PARAMS there.robmccoll about 20 hours ago |
jcalvinowens about 17 hours ago |
flohofwoe about 19 hours ago |
I've been using this style for almost 10 years now in the sokol headers, and it still feels like the right thing to do and actually more ergonomic than most other languages for initializing complex structs, e.g. see here for creating a 3D render pipeline object with dozens of creation options, but most of them keeping their default values:
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[ATTR_texcube_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_texcube_color0].format = SG_VERTEXFORMAT_UBYTE4N,
[ATTR_texcube_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
}
},
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
.cull_mode = SG_CULLMODE_BACK,
.depth = {
.compare = SG_COMPAREFUNC_LESS_EQUAL,
.write_enabled = true
},
.label = "texcube-pipeline"
});
(or see here: https://github.com/floooh/sokol-samples/blob/11bbe1a6865e93c...)...this code snippet also makes use of C99 features that are not available in C++20 though (or most other languages), and it's really remarkable how well designed the C99 designated init feature as a whole is (even though it was an afterthought) and how badly its individual subfeatures were understood by most later languages (basically, only Odin got it "mostly right": https://github.com/floooh/sokol-odin/blob/7d47a31cad83a26ba1...)
nixpulvis about 17 hours ago |
cesaref about 17 hours ago |
coldpie about 19 hours ago |