The most confusing error message

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Jan 24 13:36:10 UTC 2018


On Wednesday, January 24, 2018 15:15:30 Shachar Shemesh via Digitalmars-d 
wrote:
> On 24/01/18 10:01, Jonathan M Davis wrote:
> > On Wednesday, January 24, 2018 09:21:09 Shachar Shemesh via
> > Digitalmars-d
> >
> > wrote:
> >> test.d(6): Error: struct test.A(int var = 3) is used as a type
> >>
> >> Of course it is. That's how structs are used.
> >>
> >> Program causing this:
> >> struct A(int var = 3) {
> >>
> >>       int a;
> >>
> >> }
> >>
> >> void main() {
> >>
> >>       A a;
> >>
> >> }
> >>
> >> To resolve, you need to change A into A!(). For some reason I have not
> >> been able to fathom, default template parameters on structs don't work
> >> like they do on functions.
> >
> > It's because in the general case, it would be ambiguous. For instance,
> > what would this mean?
> >
> > alias B = A;
> >
> > Right now, B is an alias of the template A, but if we had implicit
> > instantiation for types, B would be ambiguous.
>
> So there is a reason this is an error. Fine. Is there also a reason why
> the error message doesn't say "Cannot instantiate template test.A(int
> var = 3) with no arguments" or something?
>
> Right now, the error says that "Struct (something) is used as a type",
> to which my instinctive response is "yeah, why is that an error?"

Then open a bugzilla issue about how bad the error message is. It's probably
a result of whatever code generating the error message being shared between
explicit templates and other sorts of templates where the template keyword
isn't used, and it doesn't handle the non-explicit templates very well.
Walter is quite open to improving error messages, but usually, the complaint
is that error messages aren't good enough without any examples given, and he
wants specific examples. You have one, so he (or one of the other compiler
devs) should be able to fix it. You should always feel free to report error
messages that you think are bad.

- Jonathan M Davis



More information about the Digitalmars-d mailing list