News for hackers. Stuff that matters.
Re:Facil.io: A micro-framework for C web applications by tom_mellior on Saturday January 26, 2019 @04:10AM (#19005566)
I like the use of keyword argument syntax to specify optional arguments. I haven't seen anything like this before in C or C++, and now I would wish for it to become standard.

It's simulated cleverly using a macro that wraps variable arguments in a struct literal: See the definition of http_set_cookie in https://github.com/boazsegev/facil.io/blob/master/lib/facil/...

Re:Facil.io: A micro-framework for C web applications by mafm on Saturday January 26, 2019 @08:00AM (#19006100)
I think this trick comes from Ben Klemens' "21st Century C".

If you like this, the book has some other tricks in it you might also like. (One example: you can define generic functions in C11).

Re:Facil.io: A micro-framework for C web applications by saagarjha on Saturday January 26, 2019 @08:00AM (#19006098)
> I haven't seen anything like this before in C or C++, and now I would wish for it to become standard.

I've seen people try to simulate this in C++ with tags.

Re:Facil.io: A micro-framework for C web applications by drudru11 on Saturday January 26, 2019 @02:38PM (#19007980)
Yes, this exactly - as soon as I saw that, I was like “how did he pull that off?”.

Of course, the first comment on HN has the answer.

I am going to try this trick out today.

Re:Facil.io: A micro-framework for C web applications by drudru11 on Saturday January 26, 2019 @05:41PM (#19009022)
The exact line is here:

https://github.com/boazsegev/facil.io/blob/master/lib/facil/...

I've seen other hacks to do this, but this is the cleanest that I've seen. He uses the same name for the macro and the function.

The only thing I would change is the way the struct is passed. I would pass by reference.

I really like this.

Re:Facil.io: A micro-framework for C web applications by tom_mellior on Sunday January 27, 2019 @10:12AM (#19011966)
> I would pass by reference.

Interesting. I expected taking the address of a struct literal to be illegal, but GCC and Clang both accept it. It does make sense, I guess.

But may I ask why you would pass it by reference? With old ABIs, the struct would be passed on the stack (i.e., by reference) anyway, and with more recent ones, a small prefix would be in registers but the rest passed by reference as well. So why bother manually cluttering the code?

Re:Facil.io: A micro-framework for C web applications by jolmg on Saturday January 26, 2019 @11:17AM (#19006875)
That's really cool, but don't unassigned fields have undefined values? How would the function know what was set? I imagine that in practice they're almost always 0, but that's not guaranteed, right? You'd need to set the struct as {0}, to have everything be 0. Is this a feature of a new(ish) standard?
Re:Facil.io: A micro-framework for C web applications by hawski on Saturday January 26, 2019 @11:47AM (#19007033)
It is guaranteed per C99. It's a compound initializer.

Also if you set a struct with {1} the first field is set to 1 and the rest to 0. AFAIR it was always like that (at least since standardized C, but probably earlier).

Re:Facil.io: A micro-framework for C web applications by loeg on Saturday January 26, 2019 @12:25PM (#19007227)
C99 specifies that struct literals with absent fields are assigned a zero value. (Notably unspecified is the value of any implicit padding bytes in the struct.)