The most confusing error message

Shachar Shemesh shachar at weka.io
Wed Jan 24 13:15:30 UTC 2018


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?"

Shachar


More information about the Digitalmars-d mailing list