Hacker News new | ask | show | jobs
by kayamon 3619 days ago
A lot of their "improvements" could be done just by using C99 designated initializers.
2 comments

I don't know why this is downvoted. Several of the author's claims about C are just wrong. Modern C compilers can warn you about most of these, and literal initializers eliminate the missed zero initializations.
And why are they claiming C doesn't have type-safe enums?
Because C doesn't have type-safe enums.

edit: for the downvoters, I'd LOVE to be happily corrected with a link to documentation about C language support for a type safe enum.

It isn't type-safe in the sense you're used to in C++. But common pitfalls can be avoided if you don't destroy the type information by assigning through a vanilla integer.

    enum a { A };
    enum b { B };
    
    static enum a a = B;
$ clang -Wenum-conversion -Werror a.c

    b.c:4:19: error: implicit conversion from enumeration type 'enum b' to different enumeration type 'enum a' [-Werror,-Wenum-conversion]
    static enum a a = B;
                  ~   ^
    1 error generated.
The easy ability to assign it to a vanilla integer or cast it implicitly (through an arithmetic operator or comparison for example) is what I mean when I say they aren't typesafe. A warning is also a fair bit weaker than a flat out compile error, although I'll grant that compiling with -Werror (a really challenging thing for most companies) alleviates that.
Yeah, it isn't real type safety. That said, don't assign enums through vanilla integers if you don't have to. :-)

Our engineering organization of 300+ people builds the majority of our code with -Werror (and a bunch of warnings enabled). Just an anecdote, of course.