News for hackers. Stuff that matters.
Re:Show HN: Smart pointers for the C programming language by kazinator on Thursday January 15, 2015 @04:12PM (#8895379)
Although the designated initializer has the value that it shows the name of each member, so that it resembles a keyword parameter list (like what is available ins ome languages: Common Lisp, Python, ...) it is terribly weak in that there is no logic for defaulting anything other than to zero. You have no way to express that, for instance, .handler is to be mandatory. Or if it is not mandatory, that it should default to "dfl_button_callback" or whatever.

Another thing you can't do is distinguish whether x was initialized to zero by default, or whether it was an explicit .x = 0. With the macro we know that since x isn't defaulted, it came from the macro. Or if it is defaulted, it couldn't have come from the macro. In the keyword arguments of the Lisp language, we know whether or not a keyword argument which has a default value was explicitly passed with that same value, or whether we are getting that value by default.

(In C++, which is somewhat offtopic because the subject is what is possible in C) we can do something clearer, if a bit verbose, by defining some framework for combining traits. I have done this more than once. The result looks like:

    Widget *w = new Button(Label("Press me") | Enabled(true) | Callback(callback) |...));
The various traits classes have an overloaded operator | which combines traits (Traits | Traits -> Traits). The Button constructor is the Button(const Traits &) one.
Re:Show HN: Smart pointers for the C programming language by __david__ on Friday January 16, 2015 @03:01AM (#8897710)
> …it is terribly weak in that there is no logic for defaulting anything other than to zero.

That is the weakness I addressed. IE, it may seem terribly weak up front, but in practice it's really not a weakness at all.

And, if zero is the default, then it doesn't matter if someone explicitly requested the default, or got the default by omission… It's like using 'exists' in Perl. It seems like it may be important to tell the difference between 'existing as undef' vs. not existing at all, but in practice it's just not needed (I can count the times I've legitimately used 'exists' during my almost 20 years of Perl programs on just a couple fingers).