News for hackers. Stuff that matters.
Re:D as a C Replacement by quelsolaar on Tuesday July 02, 2019 @05:59PM (#20339754)
I disagree about being better then C. Compile time asserts can be created using #error in C, and having something happen out of order at the end of a scope is a terrible idea. One of the main strengths of C is that everything is explicit and nothing happens without you saying so.
Re:D as a C Replacement by ben-schaaf on Tuesday July 02, 2019 @06:53PM (#20340118)
> Compile time asserts can be created using #error

Really not the same thing. Compile time asserts allow you to check any compile time value. For instance say you were writing OpenGL code and your struct needs to have a size that is a multiple of 16 bytes. In D that's easy:

    static assert(sizeof(Data) % 16 == 0);
Re:D as a C Replacement by nn3 on Tuesday July 02, 2019 @08:37PM (#20340674)
build time asserts can be defined in C macros with some tricks. The error messages are slightly misleading, but when people look at the source code it is usually clear what is happening.

#define ASSERT_BUILD(x) \ do { char array[(x) ? 1 : -1]; (void)array; } while(0)

static char foo[10]; ASSERT_BUILD(sizeof(foo) >= 10);

Re:D as a C Replacement by ben-schaaf on Wednesday July 03, 2019 @12:07AM (#20341518)
That's a great trick and all, but it falls disappointingly short of D's `static assert`: There's no guarantee it'll ever run. The trick assumes that the C compiler will evaluate the expression at compile time, but there's every chance that it just won't and will silently fail instead.

    char * i = malloc(1);
    ASSERT_BUILD(i);
That just silently compiles and runs without triggering any failure. Whats worse is you can never be sure whether your assert is passing or the compiler decided not to run it.
Re:D as a C Replacement by nudq on Wednesday July 03, 2019 @04:45AM (#20342690)
That compiles because with gcc extensions or sufficiently recent standards, variable size arrays are now a thing. The compiler doesn't insist on a static array size. What about something like this:

    #define ASSERT_BUILD(x) extern int STATIC_ASSERT_FAILED[(x)?1:-1]
Re:D as a C Replacement by lifthrasiir on Tuesday July 02, 2019 @10:20PM (#20341076)
> The error messages are slightly misleading, [...]

A common strategy is to use a self-describing variable name like `static_assertion_failed`.

Re:D as a C Replacement by mhh__ on Tuesday July 02, 2019 @06:12PM (#20339861)
Then don't use them? They are completely explicit, you have to write scope(exit) to make anything happen.

The use of scope(exit) is actually very useful because, for example( short of using RAII) how many times do we all forget to free or close a file. Programmers make mistakes, but if you give them the tools to not fuck up as idiomatic parts of the language: They will use them.

Re:D as a C Replacement by Skunkleton on Tuesday July 02, 2019 @06:22PM (#20339918)
> One of the main strengths of C is that everything is explicit and nothing happens without you saying so.

That's not strictly true. For example, complex object assignment _might_ be turned into a function call by the compiler.