www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static assert("nothing")

reply realhet <real_het hotmail.com> writes:
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?
May 31 2022
next sibling parent bauss <jj_1337 live.dk> writes:
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?
I agree that static assert should have a special case for handling strings, so we have both these signatures: `static assert(value, message)` and `static assert(message)`
May 31 2022
prev sibling next sibling parent reply JG <someone somewhere.com> writes:
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?
I was going to suggest to do something like: ```d import std; string compileError(string msg) { import std.format; return format("static assert(0,%(%s%));",[msg]); } auto doGreatThings(T)(T x) { static if(is(T==int)) { return "great things!"; } else mixin(compileError("Invalid type.")); } void main() { doGreatThings!int(123).writeln; doGreatThings!string("oh dear").writeln; } ``` But (a) why should you need to and (b) this makes the message more obscure. onlineapp.d-mixin-14(14): Error: static assert: "Invalid type." onlineapp.d(20): instantiated from here: `doGreatThings!string`
May 31 2022
parent bauss <jj_1337 live.dk> writes:
On Tuesday, 31 May 2022 at 09:11:41 UTC, JG wrote:
 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?
I was going to suggest to do something like: ```d import std; string compileError(string msg) { import std.format; return format("static assert(0,%(%s%));",[msg]); } auto doGreatThings(T)(T x) { static if(is(T==int)) { return "great things!"; } else mixin(compileError("Invalid type.")); } void main() { doGreatThings!int(123).writeln; doGreatThings!string("oh dear").writeln; } ``` But (a) why should you need to and (b) this makes the message more obscure. onlineapp.d-mixin-14(14): Error: static assert: "Invalid type." onlineapp.d(20): instantiated from here: `doGreatThings!string`
And then suddenly everyone has their own version of compileError. There's no reason the compiler can't check whether the first expression given evaluates to string and if so then the first argument moves to the second argument and the first argument becomes 0. `extern (D) this(const ref Loc loc, Expression exp, Expression msg)`
May 31 2022
prev sibling next sibling parent reply Andrea Fontana <nospam example.com> writes:
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.
The first is the assert condition, the message is opt.
 I think it is because of the weird case of "every string casted 
 to bool is true".
string s1; string s2 = string.init; // Equivalent string s3 = null; // Equivalent string s4 = ""; assert(s1, "This fails"); assert(s2, "This fails too"); assert(s3, "This fails too"); assert(s4, "This pass");
 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?
Check if that string is init. Andrea
May 31 2022
parent reply realhet <real_het hotmail.com> writes:
On Tuesday, 31 May 2022 at 09:35:30 UTC, Andrea Fontana wrote:
 On Tuesday, 31 May 2022 at 08:51:45 UTC, realhet wrote:
 Check if that string is init.
assert("", "cool"); assert("ehh", "cool"); assert(string.init, "Not cool"); I feel some "JavaScript equality operator" vibes in this :D Anyways, I will be extra careful with these.
May 31 2022
parent Andrea Fontana <nospam example.com> writes:
On Tuesday, 31 May 2022 at 09:52:10 UTC, realhet wrote:
 On Tuesday, 31 May 2022 at 09:35:30 UTC, Andrea Fontana wrote:
 On Tuesday, 31 May 2022 at 08:51:45 UTC, realhet wrote:
 Check if that string is init.
assert("", "cool"); assert("ehh", "cool"); assert(string.init, "Not cool"); I feel some "JavaScript equality operator" vibes in this :D Anyways, I will be extra careful with these.
assert(string.init is null) is the answer :)
May 31 2022
prev sibling parent Tejas <notrealemail gmail.com> writes:
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"); } } ```
May 31 2022