Thread overview
Compiling with unknown class gives no error
Mar 17, 2014
Duarte
Mar 17, 2014
bearophile
Mar 18, 2014
Duarte
March 17, 2014
Hi fellow coders,

While fooling around, I came across something I can't explain. Hope you can bring some light to this matter.
Let's say I have the following class definition:

class A
{
    /*
        Some stuff here
    */

    // B is a class defined nowhere, it just doesn't exist!
    B opBinary(string op)(float f)
    {
        return B();
    }
};


Compiling this with dmd/ldc will procude no error whatsoever, not even a warning (with -w/-wi enabled). It will only trigger a compile error if the operator is called at some point.

How come? Obviously if the class does not exist the compiler shouldn't be able to determine the return value of the overloaded operator. Unless there is some magic going on behind the scenes...

Just out of curiosity really. I usually avoid returning types that do not exist :)

Cheers!
March 17, 2014
Duarte:

> Compiling this with dmd/ldc will procude no error whatsoever, not even a warning (with -w/-wi enabled). It will only trigger a compile error if the operator is called at some point.
>
> How come? Obviously if the class does not exist the compiler shouldn't be able to determine the return value of the overloaded operator.

Similar questions are better asked in D.learn.

opBinary is not a method, it's a method template. So until you instantiate it, it doesn't actually exist. For some reasons for templates like C++/D ones you can't perform a full analysis of un-instantiated templated code. Time ago some people have asked for a partial analysis of template code, but Andrei was against it.

Extra note: don't end D class definitions (and structs/enums) with a semicolon.

Bye,
bearophile
March 18, 2014
On Monday, 17 March 2014 at 22:44:00 UTC, bearophile wrote:
> Duarte:
>
>> Compiling this with dmd/ldc will procude no error whatsoever, not even a warning (with -w/-wi enabled). It will only trigger a compile error if the operator is called at some point.
>>
>> How come? Obviously if the class does not exist the compiler shouldn't be able to determine the return value of the overloaded operator.
>
> Similar questions are better asked in D.learn.
>
> opBinary is not a method, it's a method template. So until you instantiate it, it doesn't actually exist. For some reasons for templates like C++/D ones you can't perform a full analysis of un-instantiated templated code. Time ago some people have asked for a partial analysis of template code, but Andrei was against it.
>
> Extra note: don't end D class definitions (and structs/enums) with a semicolon.
>
> Bye,
> bearophile

Fair enough. Thanks dude!