On Friday, 29 January 2016 at 15:00:59 UTC, pineapple wrote:
With this bit of code, the first method seems to work fine - things go as expected. But I get a compile error with the second method, and I'm not sure how else to write this.

    override bool opEquals(Object value) const{
        return this.equals(cast(typeof(this)) value);
    }
    override bool opEquals(T)(T value) const if(isNumeric!(T)){
        return this.equals(value);
    }

The compile errors I get look like this:

C:\path\to\file.d(477): Error: function units.quantity.opEquals!int.opEquals cannot override a non-virtual function C:\path\to\file.d(519): Error: template instance units.quantity.opEquals!int error instantiating

How can I revise this so that I can override comparison operators for things other than class instances?

The first implementation is fine because you're overriding the implementation in the base class (Object). However, the second one fails because it's a template. Templates are non-virtual and cannot override anything. Even if you could, there is no such implementation in Object and, therefore, nothing to override.

Templated functions can be used as overloads, though, but I'm not sure off the top of my head if the compiler accepts templated opEquals on classes at all. My guess is no, but you can find out by removing the override from the template declaration. If not, you should be able to implement non-templated overloads for the types you're interested in (without the override keyword, mind you, since they won't be overriding anything).

Reply via email to