On Tuesday, 31 May 2022 at 08:51:45 UTC, realhet wrote:
Hi,

In my framework I just found a dozen of compile time error handling like:

...else static assert("Invalid type");

This compiles without error. And it was useless for detecting errors because I forgot the first "false" or "0" parameter.

I think it is because of the weird case of "every string casted to bool is true".

There is an example in Phobos also: https://github.com/dlang/phobos/blob/master/std/uni/package.d
at line 8847: static assert("Unknown normalization form "~norm);

It is easy to make this mistake, but does static assert(string) has any meaningful use cases?

This is what Zig does, actually:

https://ziglang.org/documentation/0.9.1/#compileError

```Zig
const std = @import("std");
const expect = std.testing.expect;

test "comptime vars" {
    var x: i32 = 1;
    comptime var y: i32 = 1;

    x += 1;
    y += 1;

    try expect(x == 2);
    try expect(y == 2);

    if (y != 2) {
// This compile error never triggers because y is a comptime variable, // and so `y != 2` is a comptime value, and this if is statically evaluated.
        @compileError("wrong y value");
    }
}
```

Reply via email to