| |
| Posted by bauss in reply to JG | PermalinkReply |
|
bauss
| 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:
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)
|