Generic operator overloading for immutable types?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 12 12:46:18 PDT 2017


Gary Willoughby wrote:

> In the following code is there any way to make the `opBinary` method 
> generic to be able to accept immutable as well as a standard type? The 
> code currently passes the unit test but I wonder if I could get rid of 
> the duplication to overload the operator? I'm failing badly.

	public inout(Rational) opBinary(string op)(inout(Rational) rhs) inout {
	  static if (op == "+") {
	    return Rational(0, 0);
	  } else {
	    static assert(0, "Operator '" ~ op ~ "' not implemented");
	  }
	}


More information about the Digitalmars-d-learn mailing list