Background
A common use case for @compileError is to print error messages regarding incorrect types in generic code. For instance, an error message from std.Io.Writer.print might read something like this:
error: expected numeric type for 'd' format, found: main.MyStruct
Right now, the only real way to do this is with @typeName and string concatenation, for instance:
@compileError("expected numeric type for 'd' format, found: "++@typeName(T));
This works, but it isn't ideal:
- The compile error lacks the "type declared here" error note which built-in errors would usually emit
- The behavior of
@typeName cannot be precisely specified in the Zig language specification, so it may be desirable to modify its behavior in the future, or even remove it entirely
- String concatenation can be a little bit visually noisy to read compared with alternatives
The first point above is the main one this proposal is trying to fix---the other issues are just nice bonuses.
Proposal
Make @compileError variadic. Arguments can either have type []const u8 (with appropriate coercions of course) or type. The error to be printed is constructed by concatenating the argument strings together, converting types to their names (as with @typeName and compiler-generated errors).
The above snippet would therefore be written as follows:
@compileError("expected numeric type for 'd' format, found: ",T);
This solves each of the issues I listed above. Most importantly, the error message will include a note mentioning where the type was declared:
error: expected numeric type for 'd' format, found: main.MyStruct
note: struct 'main.MyStruct' declared here
## Background
A common use case for `@compileError` is to print error messages regarding incorrect types in generic code. For instance, an error message from `std.Io.Writer.print` might read something like this:
```
error: expected numeric type for 'd' format, found: main.MyStruct
```
Right now, the only real way to do this is with `@typeName` and string concatenation, for instance:
```zig
@compileError("expected numeric type for 'd' format, found: " ++ @typeName(T));
```
This works, but it isn't ideal:
* The compile error lacks the "type declared here" error note which built-in errors would usually emit
* The behavior of `@typeName` cannot be precisely specified in the Zig language specification, so it may be desirable to modify its behavior in the future, or even remove it entirely
* String concatenation can be a little bit visually noisy to read compared with alternatives
The first point above is the main one this proposal is trying to fix---the other issues are just nice bonuses.
## Proposal
Make `@compileError` variadic. Arguments can either have type `[]const u8` (with appropriate coercions of course) *or* `type`. The error to be printed is constructed by concatenating the argument strings together, converting types to their names (as with `@typeName` and compiler-generated errors).
The above snippet would therefore be written as follows:
```zig
@compileError("expected numeric type for 'd' format, found: ", T);
```
This solves each of the issues I listed above. Most importantly, the error message will include a note mentioning where the type was declared:
```
error: expected numeric type for 'd' format, found: main.MyStruct
note: struct 'main.MyStruct' declared here
```