Thread overview
Operator Overloading in an Abstract Base Class
Nov 20, 2014
Nordlöw
Nov 20, 2014
Nordlöw
Nov 20, 2014
Ali Çehreli
Nov 20, 2014
IgorStepanov
Nov 21, 2014
Nordlöw
November 20, 2014
In my module symbolic.d at

https://github.com/nordlow/justd/blob/master/symbolic.d

is it somehow possible to overload binary operator ~ on pairs of instances SPatt?

I'm asking because my current try at

https://github.com/nordlow/justd/blob/master/symbolic.d#L332

is not called in the unittest at

https://github.com/nordlow/justd/blob/master/symbolic.d#L347

Isn't it possible to do operator overloading in an abstract base-class?
November 20, 2014
On Thursday, 20 November 2014 at 22:10:28 UTC, Nordlöw wrote:
> In my module symbolic.d at
>
> https://github.com/nordlow/justd/blob/master/symbolic.d
>
> is it somehow possible to overload binary operator ~ on pairs of instances SPatt?

As usual I had done a couple of mistakes...which are solved know at

https://github.com/nordlow/justd/blob/master/symbolic.d#L63

Why can't I find these bugs before I do my posts? :)
November 20, 2014
On Thursday, 20 November 2014 at 22:10:28 UTC, Nordlöw wrote:
> In my module symbolic.d at
>
> https://github.com/nordlow/justd/blob/master/symbolic.d
>
> is it somehow possible to overload binary operator ~ on pairs of instances SPatt?
>
> I'm asking because my current try at
>
> https://github.com/nordlow/justd/blob/master/symbolic.d#L332
>
> is not called in the unittest at
>
> https://github.com/nordlow/justd/blob/master/symbolic.d#L347
>
> Isn't it possible to do operator overloading in an abstract base-class?

Seq opBinary(string op)(Patt rhs)
{
    static if (op == "~")
    {
        return seq(this, rhs);
    }
    else
    {
        static assert(false, "Unsupported binary operator " ~ op);
    }
}

As far I understood your question, you want to override opBinary in a derived class?
You can't do it directly, because opBinary is template and template can not be overriten. However, you may declare virtual non-template method for "~" and call it from opBinary.

And `static if (op == "~")` is not good. Use template constraint instead:

class Patt
{
    // It is better then your static if.
    // Compiler will raise better error message for incorrect operator.
    Seq opBinary(string op)(Patt rhs) if (op == "~")
    {
        return opCatImpl(rhs);
    }

    protected Seq opCatImpl(Patt rhs) //you may override it in derived class
    {
        return seq(this, rhs);
    }
}
November 20, 2014
On 11/20/2014 02:18 PM, "Nordlöw" wrote:
> On Thursday, 20 November 2014 at 22:10:28 UTC, Nordlöw wrote:
>> In my module symbolic.d at
>>
>> https://github.com/nordlow/justd/blob/master/symbolic.d
>>
>> is it somehow possible to overload binary operator ~ on pairs of
>> instances SPatt?
>
> As usual I had done a couple of mistakes...which are solved know at
>
> https://github.com/nordlow/justd/blob/master/symbolic.d#L63
>
> Why can't I find these bugs before I do my posts? :)

Glad that it works. :)

Not directly related to your post but just because your post reminded me, there is the following feature that simplifies matters when one needs virtual operators:

  template opOpAssign(string op) if (op == "~")
  { alias concatAssign opOpAssign; }

Here is the enhancement request:

  https://issues.dlang.org/show_bug.cgi?id=5893

And here is the implementation including the tests:


https://github.com/D-Programming-Language/dmd/pull/989/files#diff-51d0a1ca6214e6a916212fcbf93d7e40R246

Ali

November 21, 2014
On Thursday, 20 November 2014 at 22:35:51 UTC, IgorStepanov wrote:
> class Patt
> {
>     // It is better then your static if.
>     // Compiler will raise better error message for incorrect operator.
>     Seq opBinary(string op)(Patt rhs) if (op == "~")
>     {
>         return opCatImpl(rhs);
>     }
>
>     protected Seq opCatImpl(Patt rhs) //you may override it in derived class
>     {
>         return seq(this, rhs);
>     }
> }

Great! I've update my code! Thanks!